如何在android recyclerview中获取被点击项目的文本

时间:2017-01-08 06:34:22

标签: java android android-fragments android-recyclerview

我正在使用volley,fragment和recyclerview开发一个显示包含文本和图像的项目列表的应用程序。列表显示成功。但我想知道如何实现单击我想要在列表中获取名称的产品(ImageTitleNameView在我的代码中进行了重复)。下面显示了我的代码。

FriendsFragment.java

public class FriendsFragment extends android.support.v4.app.Fragment {



List<GetDataAdapter> GetDataAdapter1;

RecyclerView recyclerView;

RecyclerView.LayoutManager recyclerViewlayoutManager;

RecyclerView.Adapter recyclerViewadapter;

String GET_JSON_DATA_HTTP_URL = "http://192.168.xx.x/1.php";
String JSON_IMAGE_TITLE_NAME = "image_title";
String JSON_IMAGE_URL = "image_url";

JsonArrayRequest jsonArrayRequest ;

RequestQueue requestQueue ;

public FriendsFragment() {
    // Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


}

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


    GetDataAdapter1 = new ArrayList<>();
    recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerview1);
    recyclerView.setHasFixedSize(true);
    recyclerViewlayoutManager = new LinearLayoutManager(getContext());
    recyclerView.setLayoutManager(recyclerViewlayoutManager);






    JSON_DATA_WEB_CALL();



    // Inflate the layout for this fragment
    return rootView;
}

public void JSON_DATA_WEB_CALL(){

    jsonArrayRequest = new JsonArrayRequest(GET_JSON_DATA_HTTP_URL,

            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {

                    Log.d("LOGTAG", "Response :"+response);

                    JSON_PARSE_DATA_AFTER_WEBCALL(response);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });

    requestQueue = Volley.newRequestQueue(getContext());

    requestQueue.add(jsonArrayRequest);
}


public void JSON_PARSE_DATA_AFTER_WEBCALL(JSONArray array){

    for(int i = 0; i<array.length(); i++) {

        GetDataAdapter GetDataAdapter2 = new GetDataAdapter();

        JSONObject json = null;
        try {

            json = array.getJSONObject(i);

            GetDataAdapter2.setImageTitleNamee(json.getString(JSON_IMAGE_TITLE_NAME));

            GetDataAdapter2.setImageServerUrl(json.getString(JSON_IMAGE_URL));

        } catch (JSONException e) {

            e.printStackTrace();
        }
        GetDataAdapter1.add(GetDataAdapter2);
    }

    recyclerViewadapter = new RecyclerViewAdapter(GetDataAdapter1, getContext());

    recyclerView.setAdapter(recyclerViewadapter);

}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
}

@Override
public void onDetach() {
    super.onDetach();
}
}

GetDataAdapter.java

public class GetDataAdapter {

public String ImageServerUrl;
public String ImageTitleName;

public String getImageServerUrl() {
    return ImageServerUrl;
}

public void setImageServerUrl(String imageServerUrl) {
    this.ImageServerUrl = imageServerUrl;
}

public String getImageTitleName() {
    return ImageTitleName;
}

public void setImageTitleNamee(String Imagetitlename) {
    this.ImageTitleName = Imagetitlename;
}

}

SampleHolder.java

public class SampleHolder extends RecyclerView.ViewHolder {
public SampleHolder(View itemView) {
    super(itemView);
}
}

RecyclerViewAdapter.java

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

Context context;

List<GetDataAdapter> getDataAdapter;

ImageLoader imageLoader1;

private final View.OnClickListener mOnClickListener = new MyOnClickListener();


public RecyclerViewAdapter(List<GetDataAdapter> getDataAdapter, Context context){

    super();
    this.getDataAdapter = getDataAdapter;
    this.context = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items, parent, false);
    v.setOnClickListener(mOnClickListener);
    ViewHolder viewHolder = new ViewHolder(v);

        return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder Viewholder, int position) {

    GetDataAdapter getDataAdapter1 =  getDataAdapter.get(position);

    Viewholder.ImageTitleNameView.setText(getDataAdapter1.getImageTitleName());
    Log.d("LOGTAG", "Name :"+getDataAdapter1.getImageTitleName());

    imageLoader1 = ServerImageParseAdapter.getInstance(context).getImageLoader();

    imageLoader1.get(getDataAdapter1.getImageServerUrl(),
            ImageLoader.getImageListener(
                    Viewholder.networkImageView,//Server Image
                    R.mipmap.ic_launcher,//Before loading server image the default showing image.
                    android.R.drawable.ic_dialog_alert //Error image if requested image dose not found on server.
            )
    );

    Viewholder.networkImageView.setImageUrl(getDataAdapter1.getImageServerUrl(), imageLoader1);

    Viewholder.ImageTitleNameView.setText(getDataAdapter1.getImageTitleName());

}

@Override
public int getItemCount() {

    return getDataAdapter.size();
}

class ViewHolder extends RecyclerView.ViewHolder{



    public TextView ImageTitleNameView;
    public NetworkImageView networkImageView ;

    public ViewHolder(View itemView) {

        super(itemView);

        ImageTitleNameView = (TextView) itemView.findViewById(R.id.textView_item) ;

        networkImageView = (NetworkImageView) itemView.findViewById(R.id.VollyNetworkImageView1) ;

    }
}

private class MyOnClickListener implements View.OnClickListener {

    @Override
    public void onClick(View v) {


        //Log.d("LOGTAG", "clicked : name "+ );

    }
}
}

ServerImageParseAdapter.java

public class ServerImageParseAdapter {

public static ServerImageParseAdapter SIAdapter;

public static Context context1;

public RequestQueue requestQueue1;

public ImageLoader Imageloader1;

public Cache cache1 ;

public Network networkOBJ ;

LruCache<String, Bitmap> LRUCACHE = new LruCache<String, Bitmap>(30);

private ServerImageParseAdapter(Context context) {

    this.context1 = context;

    this.requestQueue1 = RQ();

    Imageloader1 = new ImageLoader(requestQueue1, new ImageLoader.ImageCache() {

        @Override
        public Bitmap getBitmap(String URL) {

            return LRUCACHE.get(URL);
        }

        @Override
        public void putBitmap(String url, Bitmap bitmap) {

            LRUCACHE.put(url, bitmap);
        }
    });
}

public ImageLoader getImageLoader() {

    return Imageloader1;
}

public static ServerImageParseAdapter getInstance(Context SynchronizedContext) {

    if (SIAdapter == null) {

        SIAdapter = new ServerImageParseAdapter(SynchronizedContext);
    }
    return SIAdapter;
}

public RequestQueue RQ() {

    if (requestQueue1 == null) {

        cache1 = new DiskBasedCache(context1.getCacheDir());

        networkOBJ = new BasicNetwork(new HurlStack());

        requestQueue1 = new RequestQueue(cache1, networkOBJ);

        requestQueue1.start();
    }
    return requestQueue1;
}
}

2 个答案:

答案 0 :(得分:5)

  

我想知道如何实现点击我想要的产品   获取列表中的名称

因为点击监听器设置为recyclerview布局而其他视图是recyclerview视图的子视图。 onClick方法的参数是The view that was clicked.

的实例

使用v访问点击的行TextView数据:

@Override
public void onClick(View v) {
     TextView textView = (TextView) v.findViewById(R.id.textView_item) ;
     Log.d("LOGTAG", "clicked : name "+textView.getText().toString() );
}

答案 1 :(得分:0)

class ViewHolder extends RecyclerView.ViewHolder{

    public TextView imageTitleNameView;
    public NetworkImageView networkImageView ;

    public ViewHolder(View itemView) {

        super(itemView);

        imageTitleNameView = (TextView) itemView.findViewById(R.id.textView_item) ;

        networkImageView = (NetworkImageView) itemView.findViewById(R.id.VollyNetworkImageView1) ;

        networkImageView.setOnClickListener(..);
        imageTitleNameView.setOnClickListener(..);

    }
}