gpt4 book ai didi

java - 如何实现这个 View

转载 作者:行者123 更新时间:2023-12-01 06:22:10 27 4
gpt4 key购买 nike

我创建了一个应用程序,它使用 recyclerviewadapter 在 gridview 中显示内部存储中的所有文件。但我不确定如何为不同的文件类型显示不同的图标。我想实现一些this kind of view 。就像文件夹有不同的图标、pdf 文件或图像有不同的图标一样。我怎样才能实现这个目标?

InternalStorage.java:

public class InternalStorage extends AppCompatActivity implements MyRecyclerViewAdapter.ItemClickListener {
MyRecyclerViewAdapter adapter;
private ArrayList<String> myList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_internal_storage);

myList = new ArrayList<>();

String path= Environment.getExternalStorageDirectory().getAbsolutePath();
File f = new File(path);//converted string object to file
File list[] = f.listFiles();//getting the list of files in string array

for( int i=0; i< list.length; i++) {

myList.add(list[i].getName());

}

// set up the RecyclerView
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rvNumbers);
int numberOfColumns = 4;
recyclerView.setLayoutManager(new GridLayoutManager(this, numberOfColumns));
adapter = new MyRecyclerViewAdapter(this, myList);
adapter.setClickListener(this);
recyclerView.setAdapter(adapter);
}
}

MyRecyclerViewAdapter.java:

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {

private ArrayList<String> mData;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;

// data is passed into the constructor
public MyRecyclerViewAdapter(Context context, ArrayList<String> data) {
this.mInflater = LayoutInflater.from(context);
this.mData = data;
}

// inflates the cell layout from xml when needed
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.recyclerview_item, parent, false);
return new ViewHolder(view);
}

// binds the data to the textview in each cell
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
String animal = mData.get(position);
holder.myTextView.setText(animal);
}

// total number of cells
@Override
public int getItemCount() {
return mData.size();
}


// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView myTextView;
ImageButton myImage;

ViewHolder(View itemView) {
super(itemView);
myTextView = (TextView) itemView.findViewById(R.id.info_text);
myImage = (ImageButton) itemView.findViewById(R.id.buttonimage);
itemView.setOnClickListener(this);
}

@Override
public void onClick(View view) {
if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
}
}

// convenience method for getting data at click position
public String getItem(int id) {
return mData.get(id);
}

// allows clicks events to be caught
public void setClickListener(ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}

// parent activity will implement this method to respond to click events
public interface ItemClickListener {
void onItemClick(View view, int position);
}
}

最佳答案

首先,您需要创建一个模型,以便将每种动物与其他动物区分开来。您可以使用具有名称和 imageUrl 的模型来完成此操作(每个动物的图像 url,您可以根据动物的类型进行设置)。之后,您必须定义一个将拥有新图像的 imageView(将使用您从 url 带来的图像进行膨胀的图像)。要加载图像,我推荐您使用 Picasso,它是一个可以更轻松地加载任何类型图像的库。

代码是这样的

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {

private ArrayList<Animal> mData;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;

// data is passed into the constructor
public MyRecyclerViewAdapter(Context context, ArrayList<String> data) {
this.mInflater = LayoutInflater.from(context);
this.mData = data;
}

// inflates the cell layout from xml when needed
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.recyclerview_item, parent, false);
return new ViewHolder(view);
}

// binds the data to the textview in each cell
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Animal animal = mData.get(position);
holder.myTextView.setText(animal);
Picasso.with(context).load(animal.imageUrl).into(holder.imageViewYouWantToChangue)
}

// total number of cells
@Override
public int getItemCount() {
return mData.size();
}


// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView myTextView;
ImageButton myImage;

ViewHolder(View itemView) {
super(itemView);
myTextView = (TextView) itemView.findViewById(R.id.info_text);
myImage = (ImageButton) itemView.findViewById(R.id.buttonimage);
imageViewYouWantToChangue = (ImageView) itemView.findViewById(R.id.image_you_to_changue);
itemView.setOnClickListener(this);
}

@Override
public void onClick(View view) {
if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
}
}

// convenience method for getting data at click position
public String getItem(int id) {
return mData.get(id);
}

// allows clicks events to be caught
public void setClickListener(ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}

// parent activity will implement this method to respond to click events
public interface ItemClickListener {
void onItemClick(View view, int position);
}
}}

public class Animal{
String name;
String imageUrl;

public Animal(String name, String type) {
this.name = name;
this.imageUrl = imageUrl;
}
}

关于毕加索的信息 http://square.github.io/picasso/

并且不要忘记在 list 中将 INTERNET_PERMISION 添加到您的应用中。

关于java - 如何实现这个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48709189/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com