如何实现这一观点

时间:2018-02-09 15:34:45

标签: java android android-layout android-recyclerview internal-storage

我创建了一个应用程序,它使用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);
}
}

1 个答案:

答案 0 :(得分:-2)

首先,您需要创建一个模型,而不是允许您将每个动物与其他动物区分开来。您可以使用具有名称和imageUrl的模型(每个动物的图像URL,而不是根据动物的类型设置)。之后,您必须定义一个将拥有新图像的imageView(将使用您从网址带来的图像进行填充)。为了加载图像,我建议你使用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/

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