我想从列表视图创建多个活动,但是为此,我需要创建n个新活动。假设我在列表视图中有20个项目,所以我无法创建20个新活动。因此,请帮助我打开这20个活动而不创建新的20个活动。
package com.example.grepmylinux;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private static final String TAG = "RecyclerViewAdapter";
private ArrayList<String> mTopicsNames = new ArrayList<>();
private Context mcontext;
public RecyclerViewAdapter(Context mcontext, ArrayList<String> mTopicsNames) {
this.mTopicsNames = mTopicsNames;
this.mcontext = mcontext;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
Log.d(TAG, "onBindViewHolder: called.");
holder.topics_name.setText(mTopicsNames.get(position));
holder.parent_layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: " + mTopicsNames.get(position));
Toast.makeText(mcontext, mTopicsNames.get(position), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(mcontext, topics.class);
intent.putExtra("topics_name", mTopicsNames.get(position));
mcontext.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return mTopicsNames.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView topics_name;
RelativeLayout parent_layout;
public ViewHolder(View itemView) {
super(itemView);
topics_name = itemView.findViewById(R.id.topics_name);
parent_layout = itemView.findViewById(R.id.parent_layout);
}
}
}
请检查上面的RecyclerView代码。我尝试过,但是当我单击第一个列表项时,它将把我重定向到同一页面,即循环。