ItemClickListener 在带有 onclick 的 Fragment

我一直在关注本教程:教程 使用 onclick 制作 json recyclerviewer,但我想使用 Fragments 而不是活动。


在 onClick 之前,我已经弄清楚了一切。


我的问题是setOnItemClickListener没有占用我的getActivity() 我已经尝试了很多,但我就是看不到亮光。


如果有人可以帮助我,那将是非常棒的!


我唯一真正的问题是 getActivity 在以下位置给出了一条红线:


fNieuwsAdapter.setOnItemClickListener( getActivity() );


NieuwsFragment.java


    import android.content.Intent;

    import android.os.Bundle;

    import android.support.v4.app.Fragment;

    import android.support.v4.app.FragmentActivity;

    import android.support.v7.widget.LinearLayoutManager;

    import android.support.v7.widget.RecyclerView;


    import android.view.LayoutInflater;

    import android.view.View;

    import android.view.ViewGroup;

    import android.widget.AdapterView;


    import com.android.volley.Request;

    import com.android.volley.RequestQueue;

    import com.android.volley.Response;

    import com.android.volley.VolleyError;

    import com.android.volley.toolbox.JsonObjectRequest;

    import com.android.volley.toolbox.Volley;


    import org.json.JSONArray;

    import org.json.JSONException;

    import org.json.JSONObject;

    import org.jsoup.Jsoup;


    import java.util.ArrayList;


    public class NieuwsFragment extends Fragment implements NieuwsAdapter.OnItemClickListener {

        public static final String EXTRA_URL = "nieuwsImageUrl";

        public static final String EXTRA_HEADING = "nieuwsHeading";

        public static final String EXTRA_CONTENT = "nieuwsContent";


        private RecyclerView fNieuwsRecyclerView;

        private NieuwsAdapter fNieuwsAdapter;

        private ArrayList<NieuwsItem> fNieuwsList;

        private RequestQueue fNieuwsQueue;


哆啦的时光机
浏览 158回答 2
2回答

慕田峪9158850

你不能使用NieuwsAdapter.setOnItemClickListener(getActivity());,除非你的活动实现了OnItemClickListener 接口,但我看到你的片段实现了OnItemClickListener,所以你可以像这样使用片段:fNieuwsAdapter.setOnItemClickListener(NieuwsFragment.this);顺便说一下,您通过意图发送数据:detailIntent.putExtra(EXTRA_URL, clickedItem.getImageUrl());detailIntent.putExtra(EXTRA_HEADING, clickedItem.getHeading());detailIntent.putExtra(EXTRA_CONTENT, clickedItem.getContent());您必须使用相同的参数获取它们(删除“”):&nbsp;String imageUrl = intent.getStringExtra(EXTRA_URL);&nbsp;String creatorName = intent.getStringExtra(EXTRA_HEADING);&nbsp;String likeCount = intent.getStringExtra(EXTRA_CONTENT);希望这可以帮助

慕的地8271018

改变public NieuwsAdapter(Context context, ArrayList<NieuwsItem> nieuwslist) {&nbsp; &nbsp; &nbsp; &nbsp; NieuwsContext = context;&nbsp; &nbsp; &nbsp; &nbsp; NieuwsList = nieuwslist;&nbsp; &nbsp; }到public NieuwsAdapter(Context context, ArrayList<NieuwsItem> nieuwslist, OnItemClickListener mOnClickListener) {&nbsp; &nbsp; &nbsp; &nbsp; this.NieuwsContext = context;&nbsp; &nbsp; &nbsp; &nbsp; this.NieuwsList = nieuwslist;&nbsp; &nbsp; &nbsp; &nbsp; this.NieuwsListener = mOnClickListener;&nbsp; &nbsp; }和这个itemView.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (NieuwsListener != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int position = getAdapterPosition();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (position != RecyclerView.NO_POSITION) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NieuwsListener.onItemClick(position);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });改成itemView.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NieuwsListener.onItemClick(int position);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });然后在片段中你可以这样做fNieuwsAdapter = new NieuwsAdapter(getActivity(), fNieuwsList,new NieuwsAdapter.OnItemClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onItemClick(int position)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //DoSomethingHere&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; });
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java