您好我做了一个回收站视图,我不知道如何在这些项目上配置Onclick功能,这些项目可以打开另一个活动名称(xyz.xml)
主要Activity.java
public class MainActivity extends AppCompatActivity {
List<GetDataAdapter> GetDataAdapter1;
RecyclerView recyclerView;
RecyclerView.LayoutManager recyclerViewlayoutManager;
RecyclerView.Adapter recyclerViewadapter;
String GET_JSON_DATA_HTTP_URL = "http://platinummun.com/android_login_api/ImageJsonData.php";
String JSON_IMAGE_TITLE_NAME = "image_title";
String JSON_IMAGE_URL = "image_url";
JsonArrayRequest jsonArrayRequest ;
RequestQueue requestQueue ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GetDataAdapter1 = new ArrayList<>();
recyclerView = (RecyclerView) findViewById(R.id.recyclerview1);
recyclerView.setHasFixedSize(true);
recyclerViewlayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(recyclerViewlayoutManager);
JSON_DATA_WEB_CALL();
}
public void JSON_DATA_WEB_CALL(){
jsonArrayRequest = new JsonArrayRequest(GET_JSON_DATA_HTTP_URL,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
JSON_PARSE_DATA_AFTER_WEBCALL(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue = Volley.newRequestQueue(this);
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, this);
recyclerView.setAdapter(recyclerViewadapter);
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.android_examples.recyclerviewimagelistview_android_examplescom.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
recyclerview_items.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="3dp"
card_view:contentPadding="3dp"
card_view:cardCornerRadius="3dp"
card_view:cardMaxElevation="3dp"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/VollyNetworkImageView1"
android:layout_width="150dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Image Name"
android:id="@+id/textView_item"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/VollyNetworkImageView1"
android:layout_toEndOf="@+id/VollyNetworkImageView1"
android:layout_marginLeft="20dp"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
请帮我做一下
答案 0 :(得分:0)
你可以试试这个
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private ArrayList<String> list;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView text;
public String result;
public MyViewHolder(View view) {
super(view);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//TODO startintent
}
});
}
}
public MyAdapter(ArrayList<String> list) {
this.list = list;
}
// Create new views (invoked by the layout manager)
@Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.view, parent, false);
MyViewHolder vh = new MyViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
holder.result = list.get(position);
holder.text.setText(list.get(position).getContentTitle());
}
@Override
public int getItemCount() {
return list.size();
}
}
答案 1 :(得分:0)
您可以使用ItemClickSupport类将侦听器附加到RecyclerView
中的项目答案 2 :(得分:0)
在Adapter类中,
你可以实现像这样的点击监听器
@Override
public void onBindViewHolder(final YourHolder holder, final int position) {
holder.cardView1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context,NextActivity.class);
//if you want to pass data
intent.putExtra("hi",list.get(position).getHi());
startActivity(intent);
}
});
}
答案 3 :(得分:0)
您可以在适配器中创建一个用于单击项目的接口。
例如:
interface ItemClickListener
{
void onItemClicked();
}
查看持有人:
public static class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private YourModel mItem;
private ItemClickListener mItemListener;
public ItemViewHolder(View itemView, ItemClickListener listener) {
super(itemView);
ButterKnife.bind(this, itemView);
mItemListener = listener;
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.view_id:
mItemListener.onItemClicked(); // You can send any field or model as a param here.
break;
default:
break;
}
}
}
之后,您可以在Activity / Fragment中实现此接口,并可以为click事件编写代码。