akoscz / YouTubePlaylist

A sample Android application which demonstrates the use of the YouTube Data v3 API.
Apache License 2.0
153 stars 84 forks source link

How to set focus on the whole item in the list #5

Closed payam30 closed 8 years ago

payam30 commented 9 years ago

Hi I've been searching for something like this and it's is working like a charm. Than you for your great work. However I want to take a step forward and show my items in a gridview. That's done but I want ,by defaul, to set focus on the first cell (item) in the list. and whenever you click on the item (and NOT the imagethumb) the url gets open.

I tried a lot and everytime I do it something strange happens. I would appritiate if someone could give me a hint .

Thanks in advance

akoscz commented 9 years ago

Sure I can try to help you out. Share some code so i can see what you are doing.

payam30 commented 9 years ago

Hi again Sir, It's your code with a very small modification:

package YoutubePackage;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.DecimalFormat;

import peyam.com.peyamtv.R;

/**
 * Created by peyam on 2015-08-06.
 */
public class PersianMovies extends Fragment {

    private static final String TAG = "YouTubeFragment";

    public static String YOUTUBE_PLAYLIST;
    private static final String PLAYLIST_KEY = "PLAYLIST_KEY";
    private GridView mGridView;
    private Playlist mPlaylist;
    private PlaylistAdapter mAdapter;
    private String[] videoId = null;
    private int i = 0;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View root = inflater.inflate(R.layout.youtube_videos, container, false);
        mGridView = (GridView) root.findViewById(R.id.gvMovies1);

        new GetYouTubePlaylistAsyncTask() {
            @Override
            public void onPostExecute(JSONObject result) {

                if (result == null) return;

                handlePlaylistResult(result);
            }
        }.execute(YOUTUBE_PLAYLIST, null);

        return root;
    }

    private void initListAdapter(Playlist playlist) {

        mAdapter = new PlaylistAdapter(playlist);
        mGridView.setAdapter(mAdapter);

    }

    private void handlePlaylistResult(JSONObject result) {

        try {
            if (mPlaylist == null) {
                mPlaylist = new Playlist(result);
                initListAdapter(mPlaylist);
            }

            final Playlist.Page page = mPlaylist.addPage(result);

            // fetch all the video details for the current page of Playlist Items
            new GetYouTubeVideoAsyncTask() {

                @Override
                public void onPostExecute(JSONObject result) {

                    if (result == null) {
                        return;
                    }

                    try {
                        JSONArray resultItems = result.getJSONArray("items");
                        PlaylistItem playlistItem;
                        for (int i = 0; i < page.items.size(); i++) {
                            playlistItem = page.items.get(i);
                            playlistItem.video = new Video(resultItems.getJSONObject(i));
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    // make sure the UI gets updated
                    mAdapter.notifyDataSetChanged();
                }
            }.execute(page);

            if (!mAdapter.setIsLoading(false)) {
                mAdapter.notifyDataSetChanged();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    protected class PlaylistAdapter extends BaseAdapter {
        private final DecimalFormat formatter = new DecimalFormat("#,###,###");
        private final LayoutInflater mInflater;
        private Playlist mPlaylist;
        private boolean mIsLoading = false;

        PlaylistAdapter(Playlist playlist) {

            mPlaylist = playlist;
            mInflater = getLayoutInflater(null);
        }

        public boolean setIsLoading(boolean isLoading) {

            if (mIsLoading != isLoading) {
                mIsLoading = isLoading;
                notifyDataSetChanged();
                return true;
            }
            return false;
        }

        @Override
        public int getCount() {

            return mPlaylist.getCount() + (mIsLoading ? 1 : 0);
        }

        @Override
        public PlaylistItem getItem(int i) {

            return mPlaylist.getItem(i);
        }

        @Override
        public long getItemId(int i) {

            return i;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup viewGroup) {

            if (mIsLoading && position == (getCount() - 1)) {
                return mInflater.inflate(R.layout.youtube_movies_loading, null, false);
            }

            ViewHolder viewHolder;

            if (convertView == null || convertView.getTag() == null) {

                viewHolder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.custom_row_movies, null, false);

                viewHolder.title = (TextView) convertView.findViewById(R.id.title);
                viewHolder.thumbnail = (ImageView) convertView.findViewById(R.id.ivThumb);
                viewHolder.duration = (TextView) convertView.findViewById(R.id.duration);
                viewHolder.viewCount = (TextView) convertView.findViewById(R.id.view);
                convertView.setTag(viewHolder);
            }

            viewHolder = (ViewHolder) convertView.getTag();
            final PlaylistItem item = getItem(position);
            if (item.title.length() >= 20) {
                viewHolder.title.setText(item.title.substring(0, 21));
            } else {
                viewHolder.title.setText(item.title);
            }

            // Loading Image
            Picasso.with(getActivity())
                    .load(item.thumbnailUrl)
                    .into(viewHolder.thumbnail);

            // set the click listener to play the video

            viewHolder.thumbnail.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=" + item.videoId)));

                }
            });

            // create and set the click listener for both the share icon and share text
//            View.OnClickListener shareClickListener = new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//
//
//                    Intent sendIntent = new Intent();
//                    sendIntent.setAction(Intent.ACTION_SEND);
//                    sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Watch \"" + item.title + "\" on YouTube");
//                    sendIntent.putExtra(Intent.EXTRA_TEXT, "http://www.youtube.com/watch?v=" + item.videoId);
//                    sendIntent.setType("text/plain");
//                    startActivity(sendIntent);
//                }
//            };

            if (item.video != null) {
                // set the video duration text
                viewHolder.duration.setText(item.video.duration);
                // set the video statistics
                viewHolder.viewCount.setText(formatter.format(item.video.viewCount));

            }

            // get the next playlist page if we're at the end of the current page and we have another page to get
            final String nextPageToken = mPlaylist.getNextPageToken(position);
            if (!isEmpty(nextPageToken) && position == getCount() - 1) {
                new GetYouTubePlaylistAsyncTask() {
                    @Override
                    public void onPostExecute(JSONObject result) {

                        handlePlaylistResult(result);
                    }
                }.execute(YOUTUBE_PLAYLIST, nextPageToken);

                setIsLoading(true);
            }

            return convertView;
        }

        private boolean isEmpty(String s) {

            if (s == null || s.length() == 0) {
                return true;
            }
            return false;
        }

        class ViewHolder {
            ImageView thumbnail;
            TextView title;
            TextView duration;
            TextView viewCount;

        }

    }

}
akoscz commented 8 years ago

If I understand your question right, you want the click listener to be on the entire cell in your grid view. In which case you should be able to the click listener on the convertView object

            convertView = mInflater.inflate(R.layout.custom_row_movies, null, false);
            // set the click listener to play the video
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=" + item.videoId)));
                }
            });
akoscz commented 8 years ago

Take a look at the most recent version of the code. I've updated it with usage of the RecyclerView. You can easily modify it to show a grid view.