我在来自recycleview的show detail类中遇到任何问题, recycleview是显示但细节不起作用, 我不明白如何点击回收视图中的位置,
这是我的ListBerita.java
stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("response ", response);
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("berita");
for (int a = 0; a < jsonArray.length(); a++) {
JSONObject json = jsonArray.getJSONObject(a);
HashMap<String, String> map = new HashMap<String, String>();
map.put("id_berita", json.getString("id_berita"));
map.put("id_pos", json.getString("id_pos"));
map.put("nama_pos", json.getString("nama_pos"));
map.put("gambar_berita", json.getString("gambar_berita"));
map.put("berita", json.getString("berita"));
list_data.add(map);
AdapterList adapter = new AdapterList(ListBerita.this, list_data);
beritalist.setAdapter(adapter);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
<!-- begin snippet: js hide: false console: true babel: false -->
&#13;
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(ListBerita.this, error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(stringRequest);
}
}
这是我的适配器
package baru.azoel.ayomuncak.Adapter;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
import java.util.HashMap;
import baru.azoel.ayomuncak.DetailBerita;
import baru.azoel.ayomuncak.ListBerita;
import baru.azoel.ayomuncak.R;
/**
* Created by azoel.
*/
public class AdapterList extends RecyclerView.Adapter<AdapterList.ViewHolder>{
Context context;
ArrayList<HashMap<String, String>> list_data;
public AdapterList(ListBerita beritaActivity, ArrayList<HashMap<String, String>> list_data) {
this.context = beritaActivity;
this.list_data = list_data;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_berita, null);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Glide.with(context)
.load("http://marimuncak.000webhostapp.com/images/" + list_data.get(position).get("gambar_berita"))
.crossFade()
.placeholder(R.mipmap.ic_launcher)
.into(holder.imgberita);
holder.txtnama.setText(list_data.get(position).get("nama_pos"));
holder.txtberita.setText(list_data.get(position).get("berita"));
}
@Override
public int getItemCount() {
return list_data.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView txtberita;
ImageView imgberita;
TextView txtnama;
public ViewHolder(View itemView) {
super(itemView);
txtnama = (TextView) itemView.findViewById(R.id.txtnama);
txtberita = (TextView) itemView.findViewById(R.id.txtberita);
imgberita = (ImageView) itemView.findViewById(R.id.imgberita);
}
}
}
&#13;
答案 0 :(得分:0)
在content
方法中为onBindViewHolder
itemView
如果您希望在活动/片段类而不是适配器类中添加项目单击,则有多种方法可以执行此操作。其中一个写在下面。
创建课程@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Glide.with(context)
.load("http://marimuncak.000webhostapp.com/images/" + list_data.get(position).get("gambar_berita"))
.crossFade()
.placeholder(R.mipmap.ic_launcher)
.into(holder.imgberita);
holder.txtnama.setText(list_data.get(position).get("nama_pos"));
holder.txtberita.setText(list_data.get(position).get("berita"));
holder.itemView.setOnClickListener(new View.onClickListener() {
@Override
public void onClick(View v) {
//Do something here with position
}
});
}
<强> ItemClickSupport.java 强>
ItemClickSupport
您可以将此课程用于所有public class ItemClickSupport {
private final RecyclerView mRecyclerView;
private OnItemClickListener mOnItemClickListener;
private OnItemLongClickListener mOnItemLongClickListener;
private View.OnClickListener mOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mOnItemClickListener != null) {
RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v);
mOnItemClickListener.onItemClicked(mRecyclerView, holder.getAdapterPosition(), v);
}
}
};
private View.OnLongClickListener mOnLongClickListener = new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (mOnItemLongClickListener != null) {
RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v);
return mOnItemLongClickListener.onItemLongClicked(mRecyclerView, holder.getAdapterPosition(), v);
}
return false;
}
};
private RecyclerView.OnChildAttachStateChangeListener mAttachListener
= new RecyclerView.OnChildAttachStateChangeListener() {
@Override
public void onChildViewAttachedToWindow(View view) {
if (mOnItemClickListener != null) {
view.setOnClickListener(mOnClickListener);
}
if (mOnItemLongClickListener != null) {
view.setOnLongClickListener(mOnLongClickListener);
}
}
@Override
public void onChildViewDetachedFromWindow(View view) {
}
};
private ItemClickSupport(RecyclerView recyclerView) {
mRecyclerView = recyclerView;
mRecyclerView.setTag(R.id.item_click_support, this);
mRecyclerView.addOnChildAttachStateChangeListener(mAttachListener);
}
public static ItemClickSupport addTo(RecyclerView view) {
ItemClickSupport support = (ItemClickSupport) view.getTag(R.id.item_click_support);
if (support == null) {
support = new ItemClickSupport(view);
}
return support;
}
public static ItemClickSupport removeFrom(RecyclerView view) {
ItemClickSupport support = (ItemClickSupport) view.getTag(R.id.item_click_support);
if (support != null) {
support.detach(view);
}
return support;
}
public ItemClickSupport setOnItemClickListener(OnItemClickListener listener) {
mOnItemClickListener = listener;
return this;
}
public ItemClickSupport setOnItemLongClickListener(OnItemLongClickListener listener) {
mOnItemLongClickListener = listener;
return this;
}
private void detach(RecyclerView view) {
view.removeOnChildAttachStateChangeListener(mAttachListener);
view.setTag(R.id.item_click_support, null);
}
public interface OnItemClickListener {
void onItemClicked(RecyclerView recyclerView, int position, View v);
}
public interface OnItemLongClickListener {
boolean onItemLongClicked(RecyclerView recyclerView, int position, View v);
}
}
,如下所示
recyclerViews
如果您有不同的导航手势
,您甚至可以覆盖ItemClickSupport.addTo(recyclerView).setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
@Override
public void onItemClicked(RecyclerView recyclerView, int position, View v) {
//Do something here with position
}
});
答案 1 :(得分:0)
public class AdapterList extends RecyclerView.Adapter<AdapterList.ViewHolder>{
Context context;
ArrayList<HashMap<String, String>> list_data;
public AdapterList(ListBerita beritaActivity, ArrayList<HashMap<String, String>> list_data) {
this.context = beritaActivity;
this.list_data = list_data;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_berita, null);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Glide.with(context)
.load("http://marimuncak.000webhostapp.com/images/" + list_data.get(position).get("gambar_berita"))
.crossFade()
.placeholder(R.mipmap.ic_launcher)
.into(holder.imgberita);
holder.txtnama.setText(list_data.get(position).get("nama_pos"));
holder.txtberita.setText(list_data.get(position).get("berita"));
holder.itemView.setOnClickListener(new View.onClickListener() {
@Override
public void onClick(View v) {
onItemClickListener.onItemClick(position);
}
});
}
@Override
public int getItemCount() {
return list_data.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView txtberita;
ImageView imgberita;
TextView txtnama;
public ViewHolder(View itemView) {
super(itemView);
txtnama = (TextView) itemView.findViewById(R.id.txtnama);
txtberita = (TextView) itemView.findViewById(R.id.txtberita);
imgberita = (ImageView) itemView.findViewById(R.id.imgberita);
}
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener){
this.onItemClickListener = onItemClickListener;
}
OnItemClickListener onItemClickListener;
public interface OnItemClickListener{
void onItemClick(int position);
}
}
你的活动。在Oncreate()
public class TestActivity extends AppCompatActivity implements AdapterList.OnItemClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
your_adapter.setOnItemClickListener(this);
}
@Override
public void onItemClick(int position) {
// code here
}
}