如何处理RecyclerViewHolder中的点击

时间:2016-03-15 11:57:43

标签: android android-recyclerview recycler-adapter

我是Android的新手,下面是我的RecyclerAdapter类,它扩展了RecyclerView。我试图在onClicklistener中设置RecyclerViewHolder,但我没有调用新活动,因为我似乎不知道如何从适配器获取上下文引用。任何帮助将不胜感激。

public class TabFragmentNews extends Fragment {
private static final String ARG_EXAMPLE = "this_is_a_constant";
private String example_data;
private RecyclerView recyclerView;
ProgressBar progressBar;


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_news, container, false);

    progressBar = (ProgressBar) view.findViewById(R.id.progressBarNews);
    recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview_news);
    //Executing The Async Task Class Here
    newsupdate();
    return view;
}

//Method For Executing The Async Task
public void newsupdate() {
    new BackgroundTask(getActivity()).execute();
}

//Checking for internet/network connection here


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    example_data = getArguments().getString(ARG_EXAMPLE);
    Log.i("Fragment created with ", example_data);
}

public static TabFragmentNews newInstance(String example_argmument) {
    TabFragmentNews tabFragmentNews = new TabFragmentNews();
    Bundle args = new Bundle();
    args.putString(ARG_EXAMPLE, example_argmument);
    tabFragmentNews.setArguments(args);
    return tabFragmentNews;
}

//AsynTask Class for Downloading News Information
class BackgroundTask extends AsyncTask<Void, News, Void> {

    Context ctx;
    Activity activity;
    RecyclerView.Adapter adapter;
    RecyclerView.LayoutManager layoutManager;
    ProgressDialog progressDialog;
    ArrayList<News> arrayList = new ArrayList<>();

    public BackgroundTask(Context ctx) {
        this.ctx = ctx;
        activity = (Activity) ctx;
    }

    String json_string = "http://doubleclickug.com/app/newtest.php";

    @Override
    protected void onPreExecute() {
        layoutManager = new LinearLayoutManager(ctx);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        adapter = new RecyclerAdapter(getContext(),arrayList);
        recyclerView.setAdapter(adapter);

        progressBar.setVisibility(View.VISIBLE);

    }

    @Override
    protected Void doInBackground(Void... params) {
        try {
            URL url = new URL(json_string);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder stringBuilder = new StringBuilder();
            String line;

            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line + "\n");
            }
            httpURLConnection.disconnect();
            String json_string = stringBuilder.toString().trim();
            JSONObject jsonObject = new JSONObject(json_string);
            JSONArray jsonArray = jsonObject.getJSONArray("server_response");
            int count = 0;
            while (count < jsonArray.length()) {
                JSONObject JO = jsonArray.getJSONObject(count);
                count++;
                News news = new News(JO.getString("details"));
                publishProgress(news);
                //Thread.sleep(100);
            }

            Log.d("JSON STRING", json_string);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
            //} catch (InterruptedException e) {
            // e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(News... values) {
        arrayList.add(values[0]);
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        //progressDialog.dismiss();
        progressBar.setVisibility(View.GONE);

    }
}

//RecyclerAdapter Class for populating recycler view with Information
class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder> {

    ArrayList<News> arrayList = new ArrayList<>();

    public static final int TYPE_HEAD = 0;
    public static final int TYPE_LIST = 1;
    Context context;

    public RecyclerAdapter(Context context,ArrayList<News> arrayList) {
        this.arrayList = arrayList;
    }

    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_HEAD) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_header, parent, false);
            RecyclerViewHolder recyclerViewHolder = new RecyclerViewHolder(view, viewType);
            return recyclerViewHolder;
        } else if (viewType == TYPE_LIST) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_row_layout, parent, false);
            RecyclerViewHolder recyclerViewHolder = new RecyclerViewHolder(view, viewType);
            return recyclerViewHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(final RecyclerViewHolder holder, final int position) {

        if (holder.viewType == TYPE_LIST) {
            final News news = arrayList.get(position - 1);
            holder.Details.setText(news.getDetails());
        }
    }

    @Override
    public int getItemCount() {
        return arrayList.size() + 1;
    }

    //RecyclerView Holder Class
    class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView Details;
        int viewType;

        public RecyclerViewHolder(View view, int viewType) {
            super(view);
            view.setOnClickListener(this);
            if (viewType == TYPE_LIST) {
                Details = (TextView) itemView.findViewById(R.id.team);
                this.viewType = TYPE_LIST;
            } else if (viewType == TYPE_HEAD) {
                this.viewType = TYPE_HEAD;
            }

        }

        //handling Clicks of news elements here
        @Override
        public void onClick(View v) {

            //How Do I Call an Activity???
            context.startActivity(new Intent(context,NewsActivity.class));

        }


    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0)
            return TYPE_HEAD;
        return TYPE_LIST;
    }

}

将代码编辑到上面后: - 应用程序无法导航到其他活动,点击它时会粉碎

4 个答案:

答案 0 :(得分:0)

将上下文作为参数传递给适配器的构造函数,并在调用start活动时使用它。 示例

class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder> {

ArrayList<News> arrayList = new ArrayList<>();
Context context;

public static final int TYPE_HEAD = 0;
public static final int TYPE_LIST = 1;

public RecyclerAdapter(Context context, ArrayList<News> arrayList) {
    this.arrayList = arrayList;
    this.context = context;
}
.....
.....


//RecyclerView Holder Class
class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView Details;
int viewType;

public RecyclerViewHolder(View view, int viewType) {
    super(view);
    view.setOnClickListener(this);
    if (viewType == TYPE_LIST) {
        Details = (TextView) itemView.findViewById(R.id.team);
        this.viewType = TYPE_LIST;
    } else if (viewType == TYPE_HEAD) {
        this.viewType = TYPE_HEAD;
    }

}

//handling Clicks of news elements here
@Override
public void onClick(View v) {
    context.startActivity(new Intent(context,ActivityTwo.class));


   }
}

来自您的活动呼叫适配器...... (对于片段使用getContext()而不是this

RecyclerAdapter adapter = new RecyclerAdapter(this,mArrayList);

答案 1 :(得分:0)

通过构造函数传递上下文并执行以下操作:

Intent intent = new Intent(context, YourActivityToStart.class);
startActivity(intent);

在您的代码中更改此内容:

public RecyclerAdapter(Context context,ArrayList<News> arrayList) {
    this.arrayList = arrayList;
}

public RecyclerAdapter(Context context,ArrayList<News> arrayList) {
    this.arrayList = arrayList;
    this.context  = context;
}

并在适配器内部更改此行:

adapter = new RecyclerAdapter(getContext(),arrayList);

adapter = new RecyclerAdapter(getActivity(),arrayList);

答案 2 :(得分:0)

  class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder> {

    ArrayList<News> arrayList = new ArrayList<>();

    public static final int TYPE_HEAD = 0;
    public static final int TYPE_LIST = 1;
    Context context;
    public RecyclerAdapter(Context context,ArrayList<News> arrayList) {
        this.arrayList = arrayList;
this.context = context;
    }

    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_HEAD) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_header, parent, false);
            RecyclerViewHolder recyclerViewHolder = new RecyclerViewHolder(view, viewType);
            return recyclerViewHolder;
        } else if (viewType == TYPE_LIST) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_row_layout, parent, false);
            RecyclerViewHolder recyclerViewHolder = new RecyclerViewHolder(view, viewType);
            return recyclerViewHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(final RecyclerViewHolder holder, final int position) {

        if (holder.viewType == TYPE_LIST) {
            News news = arrayList.get(position - 1);
            holder.Details.setText(news.getDetails());
            holder.itemView.setOnClickListener(new Listener(news));
             holder.Details.setOnClickListener(new Listener(news));
        }
    }

    @Override
    public int getItemCount() {
        return arrayList.size() + 1;
    }


    class Listener  implements View.OnClickListener
    {
    News news;
    Listener(News news)

    {
    this.news = news;
    }
     //handling Clicks of news elements here
        @Override
        public void onClick(View v) {

            //how do I call a new Activity???
    Intent intent = new Intent(context, YourActivityToStart.class);
context.startActivity(intent);
        }
    }

    //RecyclerView Holder Class
    class RecyclerViewHolder extends RecyclerView.ViewHolder {
        TextView Details;
        int viewType;
    View itemView;
        public RecyclerViewHolder(View view, int viewType) {
            super(view);
           this.itemView =view;
            if (viewType == TYPE_LIST) {
                Details = (TextView) itemView.findViewById(R.id.team);
                this.viewType = TYPE_LIST;
            } else if (viewType == TYPE_HEAD) {
                this.viewType = TYPE_HEAD;
            }

        }




    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0)
            return TYPE_HEAD;
        return TYPE_LIST;
    }

答案 3 :(得分:0)

在构造函数中创建回收器视图适配器时,请为回收器视图项接受自定义设计的接口。在这里,我为你设计了一个:

侦听器的类级变量:

private CustomItemsClickListener mCardViewClickListener;

public interface CustomItemsClickListener {

    public void onCardViewItemClicked(View itemView, int position);

//你也可以根据自己的需要添加一些参数。     }

ur recyclerview的构造函数:

public CustomRecyclerViewAdapter(ArrayList<SomedataForYourItems> dataForItemsList,                                        CustomItemsClickListener mCardViewClickListener){

    this.mCardViewClickListener = mCardViewClickListener;
    this.dataForItemsList= dataForItemsList;
}

来自recyclerView适配器类;我希望你有一个自定义视图持有者;像这样你可以打电话(一个通用的例子):

public class CustomViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

    public TextView tv1;
    public Button btnCustom1, btnCustom2;
    public CardView container;

    public CustomOrdersViewHolder(View view) {
        super(view);

        container = (CardView) view.findViewById(R.id.item_view);
        tv1= (TextView) view.findViewById(R.id.tv1);
        btnCustom1= (Button) view.findViewById(R.id.btnCustom1);
        btnCustom2= (Button) view.findViewById(R.id.btnCustom1);

        btnCustom1.setOnClickListener(this);
        btnCustom2.setOnClickListener(this);
   }

    @Override
    public void onClick(View cardViewitems) {
     mCardViewClickListener.onCardViewItemClicked(cardViewitems, getAdapterPosition());
    }
}

在来电者端你可以这样做:

mAdapter = new CustomRecyclerViewAdapter(getYourArrayListData(),
            new CustomRecyclerViewAdapter.CustomItemsClickListener () {
                @Override
                public void onCardViewItemClicked(View view, int position) {

                    //TODO cardview click
                    switch (view.getId()) {

                        case R.id.btnCustom1:
                            // TODO some actions for btn1
                            break;

                        case R.id.btnCustom2:
                            // TODO some actions for btn2
                            break;

                    }
                }
            });
    mYRecyclerView.setAdapter(mAdapter);