如何在recyclerview的每一行中获得两个不同的imageview和textview 并且我想让每个imageview可点击....我已经在列表视图中完成了这个但是不知道如何在回收站视图中这样做....所以帮助我实现这个...提前感谢< / p>
这是我的布局文件recyclerview row
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="2">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="250dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/thumbnail"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="6dp"
android:scaleType="fitXY" />
/>
<TextView
android:id="@+id/category_title_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/thumbnail"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:ellipsize="end"
android:maxLines="1"
android:text="Title"
android:textColor="@android:color/black"
android:textSize="10dp" />
<TextView
android:id="@+id/tv_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/category_title_one"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:ellipsize="end"
android:maxLines="1"
android:text="Price"
android:textColor="@android:color/black"
android:textSize="10dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/thumbnail2"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="6dp"
android:scaleType="fitXY" />
/>
<TextView
android:id="@+id/category_title_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/thumbnail2"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:ellipsize="end"
android:maxLines="1"
android:text="Title"
android:textColor="@android:color/black"
android:textSize="10dp">
</TextView>
<TextView
android:id="@+id/tv_price2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/category_title_two"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:ellipsize="end"
android:maxLines="1"
android:text="Price"
android:textColor="@android:color/black"
android:textSize="10dp">
</TextView>
</RelativeLayout>
</LinearLayout>
这是我的适配器类
public class RecyclerViewDataAdapter extends RecyclerView.Adapter<RecyclerViewDataAdapter.ViewHolder> {
private Context context;
public ImageLoader mImageLoader;
//List of superHeroes
List<ListOfData> superHeroes;
public RecyclerViewDataAdapter(List<ListOfData> superHeroes, Context context) {
super();
//Getting all the superheroes
this.superHeroes = superHeroes;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recyclerview_row, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
ListOfData superHero = superHeroes.get(position);
mImageLoader = MyApplication.getInstance().getImageLoader();
mImageLoader.get(superHero.getImageUrl(), ImageLoader.getImageListener(holder.imageView1, R.mipmap.ic_launcher, android.R.drawable.ic_dialog_alert));
mImageLoader.get(superHero.getImageUrl(), ImageLoader.getImageListener(holder.imageView2, R.mipmap.ic_launcher, android.R.drawable.ic_dialog_alert));
holder.imageView1.setImageUrl(superHero.getImageUrl(), mImageLoader);
holder.imageView2.setImageUrl(superHero.getImageUrl(), mImageLoader);
holder.textViewName1.setText(superHero.getName());
holder.textViewName2.setText(superHero.getName());
//holder.textViewRank.setText(String.valueOf(superHero.getRank()));
//holder.textViewRealName.setText(superHero.getRealName());
//holder.textViewCreatedBy.setText(superHero.getCreatedBy());
//holder.textViewFirstAppearance.setText(superHero.getFirstAppearance());
String powers = "";
/* for(int i = 0; i<superHero.getPowers().size(); i++){
powers+= superHero.getPowers().get(i);
}*/
//holder.textViewPowers.setText(powers);
}
@Override
public int getItemCount() {
return superHeroes.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
public NetworkImageView imageView1;
public NetworkImageView imageView2;
public TextView textViewName1;
public TextView textViewName2;
public TextView textViewRank;
public TextView textViewRealName;
public TextView textViewCreatedBy;
public TextView textViewFirstAppearance;
public TextView textViewPowers;
public ViewHolder(View itemView) {
super(itemView);
imageView1 = (NetworkImageView) itemView.findViewById(R.id.thumbnail);
imageView2 = (NetworkImageView) itemView.findViewById(R.id.thumbnail2);
textViewName1 = (TextView) itemView.findViewById(R.id.category_title_one);
textViewName2 = (TextView) itemView.findViewById(R.id.category_title_two);
/* textViewRank= (TextView) itemView.findViewById(R.id.textViewRank);
textViewRealName= (TextView) itemView.findViewById(R.id.textViewRealName);
textViewCreatedBy= (TextView) itemView.findViewById(R.id.textViewCreatedBy);
textViewFirstAppearance= (TextView) itemView.findViewById(R.id.textViewFirstAppearance);
textViewPowers= (TextView) itemView.findViewById(R.id.textViewPowers);
*/
}
}
}
这里是Json部分
public class Pants extends Fragment {
//Creating a List of superheroes
public Context context;
public List<ListOfData> listSuperHeroes;
//Creating Views
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter adapter;
public Pants() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.tshirts_pants_shirts, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
layoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
//Calling method to get data
getData();
return view;
}
private void getData() {
//Showing a progress dialog
final ProgressDialog loading = ProgressDialog.show(getActivity(), "Loading Data", "Please wait...", false, false);
//Creating a json array request
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(ProductConfig.DATA_URL,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
//Dismissing progress dialog
loading.dismiss();
//calling method to parse json array
parseData(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//Creating request queue
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
//Adding request to the queue
requestQueue.add(jsonArrayRequest);
}
//This method will parse json data
private void parseData(JSONArray array) {
listSuperHeroes = new ArrayList<>();
for (int i = 0; i < array.length(); i++) {
ListOfData superHero = new ListOfData();
JSONObject json = null;
try {
json = array.getJSONObject(i);
superHero.setImageUrl(json.getString(ProductConfig.TAG_IMAGE_URL));
superHero.setName(json.getString(ProductConfig.TAG_NAME));
/*superHero.setRank(json.getInt(ProductConfig.TAG_RANK));
superHero.setRealName(json.getString(ProductConfig.TAG_REAL_NAME));
superHero.setCreatedBy(json.getString(ProductConfig.TAG_CREATED_BY));
superHero.setFirstAppearance(json.getString(ProductConfig.TAG_FIRST_APPEARANCE));
*/
ArrayList<String> powers = new ArrayList<String>();
JSONArray jsonArray = json.getJSONArray(ProductConfig.TAG_POWERS);
for (int j = 0; j < jsonArray.length(); j++) {
powers.add(((String) jsonArray.get(j)) + "\n");
}
superHero.setPowers(powers);
} catch (JSONException e) {
e.printStackTrace();
}
listSuperHeroes.add(superHero);
}
adapter = new RecyclerViewDataAdapter(listSuperHeroes, context);
//Adding adapter to recyclerview
recyclerView.setAdapter(adapter);
//Initializing our superheroes list
}
}
答案 0 :(得分:0)
如果我正确理解您的问题,请在onBindViewHolder
中使用相同的数据设置TextViews
和ImageViews
。这就是为什么你两次获得相同的图像。您应该做的是将Map<int, List<Object>> superHeroes
传递到RecyclerView
。键将是行项目编号,列表将包含具有标题和图像uri的超级英雄的对象。
使您的个人ImageView 可点击
, set an
onClickListener on each
onBindViewHolder`并处理他们的尊重逻辑。
答案 1 :(得分:0)
从这段代码我可以看到你将相同的图像设置为imageView1和imageView2以及textViewName1和textViewName2的相同文本
holder.imageView1.setImageUrl(superHero.getImageUrl(), mImageLoader);
holder.imageView2.setImageUrl(superHero.getImageUrl(), mImageLoader);
holder.textViewName1.setText(superHero.getName());
holder.textViewName2.setText(superHero.getName());
如果您想要两个不同的图像,则必须设置两个不同的图像而不是相同的图像。
对于单击处理,您必须在图像/文本/按钮/视图上设置要点击的onClickListener,例如,如果您希望imageView1可单击,则必须执行
holder.imageView1.setOnClickListener(this);
比你必须实现接口
public class RecyclerViewDataAdapter extends RecyclerView.Adapter<RecyclerViewDataAdapter.ViewHolder> implements View.OnClickListener {
之后,您可以在覆盖方法中处理onClick事件
@Override
public void onClick(View v) {
}
我希望有所帮助。
答案 2 :(得分:0)
同一行的图像是属于同一个英雄还是来自不同的英雄?
如果他们来自不同的英雄:
1)将您的recyclerview行布局更改为只有1个imageview和2个textviews(删除第二个relativelayout)
2)将recyclerview的LayoutManager更改为GridLayoutManager,其中包含2列:
GridLayoutManager glm = new GridLayoutManager(getContext(), 2);
glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return 2;
}
});
recyclerView.setLayoutManager(glm);
3)相应地更改你的onBindViewHolder meethod和ViewHolder类(删除第二个imageview以及第三和第四个textview)
4)使图像可单击,将以下内容添加到onBindViewHolder方法:
holder.imageView1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
答案 3 :(得分:0)
即使这个问题已经有 4 年的历史了。我要发布一个答案,以便其他人可以看到。
由于您提供的是数组,因此位置是从数组的每个索引中获取值(图像和文本)项。
您需要做的是像您一样提供数组,但要确保数组的每个索引都包含 image1、text1、image2 和 text2。