英文:
How to add images in my Recycler view with in every line of list?
问题
我是新手Android开发者,遇到了在RecyclerView列表中添加ImageView的问题。我已经努力解决这个问题几个月了,但仍然找不到解决方案。
有没有人可以帮助我解决这个问题?以下是我的MainActivity.java文件供参考:
package com.example.customlistview;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private Context mContext;
ArrayList<String> titleArrayList;
RecyclerView mRecyclerView;
int images[] = {R.drawable.facebook, R.drawable.whatsapp, R.drawable.twitter, R.drawable.instagram, R.drawable.youtube};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
titleArrayList = new ArrayList<String>();
titleArrayList.add(Constants.NUMBER_SYSTEM);
titleArrayList.add(Constants.POWER_ICDICES);
titleArrayList.add(Constants.SIMPLIFICATION);
titleArrayList.add(Constants.ALGEBRA);
mContext = MainActivity.this;
mRecyclerView = (RecyclerView) findViewById(R.id.Recycle);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
TitleAdapter adapter = new TitleAdapter(mContext, titleArrayList, new CustomItemClickListener() {
@Override
public void onItemClick(View v, int i) {
Toast.makeText(mContext, "clicked" + titleArrayList.get(i), Toast.LENGTH_SHORT).show();
}
});
mRecyclerView.setAdapter(adapter);
}
}
TitleAdapter.java
package com.example.customlistview;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class TitleAdapter extends RecyclerView.Adapter<TitleAdapter.MyViewHolder> {
private Context mContext;
private ArrayList<String> titleList;
private CustomItemClickListener clickListener;
public TitleAdapter(Context mContext, ArrayList<String> titleList, CustomItemClickListener clickListener) {
this.mContext = mContext;
this.titleList = titleList;
this.clickListener = clickListener;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
final View view = LayoutInflater.from(mContext).inflate(R.layout.row, parent, false);
final MyViewHolder viewHolder = new MyViewHolder(view);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickListener.onItemClick(view, viewHolder.getPosition());
}
});
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.titletext.setText(titleList.get(position).replace("_", " "));
}
@Override
public int getItemCount() {
return titleList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView titletext;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
titletext = (TextView) itemView.findViewById(R.id.textView1);
}
}
}
Constants.java
package com.example.customlistview;
class Constants {
static final String NUMBER_SYSTEM = "NUMBER_SYSTEM(संख्या पद्धति)";
static final String POWER_ICDICES = "POWER,ICDICES & SURDS";
static final String SIMPLIFICATION = "SIMPLIFICATION(संख्या पद्धति)";
static final String ALGEBRA = "ALGEBRA(बीजगणित)";
}
CustomItemClickListener.java
package com.example.customlistview;
import android.view.View;
public interface CustomItemClickListener {
public void onItemClick(View v, int i);
}
这是我的row.xml文件,包含ImageView:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:contentPadding="5dp"
app:cardElevation="3dp"
app:cardCornerRadius="10dp"
app:cardUseCompatPadding="true"
app:cardBackgroundColor="@color/whiteColor">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<ImageView
android:id="@+id/image"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/whatsapp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:orientation="vertical"
android:layout_marginLeft="5dp">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:text="@string/main_title"
android:textColor="@color/blackColor"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
希望这些代码能帮助你解决在RecyclerView列表中添加ImageView的问题。如果有任何其他问题,请随时提出。
英文:
I am new to Android development and am having trouble adding an ImageView to a RecyclerView list. I have been working on this for a couple of months, but am still struggling to find a solution.
Is there anyone who could help me with this issue? Here is my MainActivity.java file for reference:
package com.example.customlistview;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private static final String TAG="MainActivicty";
private Context mcontext;
ArrayList<String> titleArrayList;
RecyclerView mRecyclerview;
int images[] = {R.drawable.facebook, R.drawable.whatsapp, R.drawable.twitter, R.drawable.instagram, R.drawable.youtube};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
titleArrayList=new ArrayList<String>();
titleArrayList.add(Constants.NUMBER_SYSTEM);
titleArrayList.add(Constants.POWER_ICDICES);
titleArrayList.add(Constants.SIMPLIFICATION);
titleArrayList.add(Constants.ALGEBRA);
;
mcontext=MainActivity.this;
mRecyclerview=(RecyclerView) findViewById(R.id.Recycle);
mRecyclerview.setHasFixedSize(true);
mRecyclerview.setItemAnimator(new DefaultItemAnimator());
mRecyclerview.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
TitleAdapter adapter=new TitleAdapter(mcontext, titleArrayList, new CustomItemClickListener() {
@Override
public void onItemClick(View v, int i) {
Toast.makeText(mcontext, "clicked"+titleArrayList.get(i),Toast.LENGTH_SHORT).show();
}
});
mRecyclerview.setAdapter(adapter);
}
}
TitleApdapter.java
package com.example.customlistview;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class TitleAdapter extends RecyclerView.Adapter<TitleAdapter.MyViewHolder> {
private Context mcontext;
private ArrayList<String> titleList;
private CustomItemClickListener clickListener;
public TitleAdapter(Context mcontext, ArrayList<String> titleList, CustomItemClickListener clickListener) {
this.mcontext = mcontext;
this.titleList = titleList;
this.clickListener = clickListener;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
final View view= LayoutInflater.from(mcontext).inflate(R.layout.row,parent,false);
final MyViewHolder viewHolder=new MyViewHolder(view);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickListener.onItemClick(view,viewHolder.getPosition());
}
});
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.titletext.setText(titleList.get(position).replace("_"," "));
}
@Override
public int getItemCount() {
return titleList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
TextView titletext;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
titletext=(TextView) itemView.findViewById(R.id.textView1);
}
}
}
Constant.java
package com.example.customlistview;
class Constants {
static final String NUMBER_SYSTEM="NUMBER_SYSTEM(संख्या पद्धति)";
static final String POWER_ICDICES="POWER,ICDICES & SURDS";
static final String SIMPLIFICATION="SIMPLIFICATION(संख्या पद्धति)";
static final String ALGEBRA="ALGEBRA(बीजगणित)";
}
CustomItemClickListener.java
package com.example.customlistview;
import android.view.View;
public interface CustomItemClickListener {
public void onItemClick(View v,int i);
}
this is my row.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:contentPadding="5dp"
app:cardElevation="3dp"
app:cardCornerRadius="10dp"
app:cardUseCompatPadding="true"
app:cardBackgroundColor="@color/whiteColor"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<ImageView
android:id="@+id/image"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/whatsapp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:orientation="vertical"
android:layout_marginLeft="5dp">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:text="@string/main_title"
android:textColor="@color/blackColor"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
Any guidance would be greatly appreciated. Thank you in advance.
答案1
得分: 0
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.recyclerview.widget.RecyclerView;
import com.example.geometry.R;
import java.util.List;
public class ListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<ItemAdapter> mList;
private Context mContext;
public ListAdapter(List<ItemAdapter> list, Context context){
super();
mList = list;
mContext = context;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
View v = LayoutInflater.from(mContext).inflate(R.layout.item_custom, parent, false);
final ViewHolder viewHolder = new ViewHolder(v);
viewHolder.mImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Input mode = " + viewHolder.getAdapterPosition(), Toast.LENGTH_SHORT).show();
Builder.mode = viewHolder.getAdapterPosition();
}
});
return viewHolder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
ItemAdapter itemAdapter = mList.get(position);
((ViewHolder) viewHolder).mImg.setImageResource(itemAdapter.getImage());
}
@Override
public int getItemCount() {
return mList.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public ImageView mImg;
public ViewHolder(View itemView) {
super(itemView);
mImg = itemView.findViewById(R.id.img_item);
}
}
}
public class ItemAdapter {
private int image;
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
}
import android.os.Bundle;
import android.widget.ImageButton;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecycleView;
private List<ItemAdapter> mList = new ArrayList<>();
private ListAdapter mAdapter;
ImageButton solve_button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
addList();
adapter();
}
private void init(){
setContentView(R.layout.activity_main);
mRecycleView = findViewById(R.id.recycler_view);
}
private void addList(){
ItemAdapter itemAdapter = new ItemAdapter();
itemAdapter.setImage(R.drawable.circle);
mList.add(itemAdapter);
// ... (similar lines for adding more items)
}
private void adapter(){
mAdapter = new ListAdapter(mList, this);
mRecycleView.setAdapter(mAdapter);
mRecycleView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
mAdapter.notifyDataSetChanged();
}
}
Please note that the XML content was not included as you requested only the translated code. If you have any further questions or need clarifications, feel free to ask.
英文:
I came across a similar problem and successfully solved it. Now I can give you my code. I think it will help you
ListAdapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.recyclerview.widget.RecyclerView;
import com.example.geometry.R;
import java.util.List;
public class ListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<ItemAdapter> mList;
private Context mContext;
public ListAdapter(List<ItemAdapter> list, Context context){
super();
mList = list;
mContext = context;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
View v = LayoutInflater.from(mContext).inflate(R.layout.item_custom, parent, false);
final ViewHolder viewHolder = new ViewHolder(v);
viewHolder.mImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Input mode = " + viewHolder.getAdapterPosition(), Toast.LENGTH_SHORT).show();
Builder.mode = viewHolder.getAdapterPosition();
}
});
return viewHolder;
}
@Override
public void onBindViewHolder( RecyclerView.ViewHolder viewHolder, int position) {
ItemAdapter itemAdapter = mList.get(position);
((ViewHolder) viewHolder).mImg.setImageResource(itemAdapter.getImage());
}
@Override
public int getItemCount() {
return mList.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView mTv_name;
public ImageView mImg;
public ViewHolder(View itemView) {
super(itemView);
mImg = (ImageView) itemView.findViewById(R.id.img_item);
}
}
}
ItemAdapter.java
public class ItemAdapter {
private int image;
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
}
MainActivity.java
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.geometry.GUI.Builder;
import com.example.geometry.GUI.ItemAdapter;
import com.example.geometry.GUI.ListAdapter;
import com.example.geometry.Output.SolveActivity;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecycleView;
private List<ItemAdapter> mList = new ArrayList<>();
private ListAdapter mAdapter;
ListView listView;
RelativeLayout layout;
ImageButton solve_button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
addList();
adapter();
}
private void init(){
setContentView(R.layout.activity_main);
layout = new RelativeLayout(this);
mRecycleView = findViewById(R.id.recycler_view);//new RecyclerView(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
mRecycleView.setLayoutParams(params);
}
private void addList(){
ItemAdapter itemAdapter = new ItemAdapter();
itemAdapter.setImage(R.drawable.circle);
mList.add(itemAdapter);
itemAdapter = new ItemAdapter();
itemAdapter.setImage(R.drawable.line);
mList.add(itemAdapter);
itemAdapter = new ItemAdapter();
itemAdapter.setImage(R.drawable.move);
mList.add(itemAdapter);
itemAdapter = new ItemAdapter();
itemAdapter.setImage(R.drawable.angle);
mList.add(itemAdapter);
itemAdapter = new ItemAdapter();
itemAdapter.setImage(R.drawable.regular_triangle);
mList.add(itemAdapter);
itemAdapter = new ItemAdapter();
itemAdapter.setImage(R.drawable.right_triangle);
mList.add(itemAdapter);
itemAdapter = new ItemAdapter();
itemAdapter.setImage(R.drawable.square);
mList.add(itemAdapter);
itemAdapter = new ItemAdapter();
itemAdapter.setImage(R.drawable.trapeze);
mList.add(itemAdapter);
}
private void adapter(){
mAdapter = new ListAdapter(mList, this);
mRecycleView.setAdapter(mAdapter);
mRecycleView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
mAdapter.notifyDataSetChanged();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="@+id/solve_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="30dp"
android:background="@drawable/ic_calculator" />
<com.example.geometry.GUI.Builder
android:id="@+id/builder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/recycler_view"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
/>
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
</RelativeLayout>
item_custom.java
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_margin="2dp"
android:background="#fff"
android:layout_height="wrap_content">
android:layout_gravity="center"
android:scaleType="center">
<ImageView
android:id="@+id/img_item"
android:layout_width="wrap_content"
android:contentDescription="@string/app_name"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:scaleType="center"
android:layout_centerInParent="true"/>
</RelativeLayout>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论