无法在recyclerview

时间:2017-09-04 14:28:28

标签: java android android-recyclerview

尝试将图像显示到recyclerview,然后将其传递给arraylist中的recyclerview,但无法显示。
我正在将图像uri传递给dataadapter并成功收到, 但仍然无法在回收站视图中显示图像 请指出错误。
图像不会显示在recyclerview布局中。

主要活动。

public class SelectedImagePreview extends AppCompatActivity {
    private BitmapFactory.Options options;
    private RecyclerView viewPager;
    private View btnNext, btnPrev;
    private DataAdapter adapter;
    private LinearLayout thumbnailsContainer;
    ArrayList<File> mainfile = new ArrayList<>();
    File allfile;
    ArrayList<String> filename = new ArrayList<String>();
    ArrayList<Uri> images_uri = new ArrayList<Uri>();
    //the images to display
    String[] imageIDs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.preview_activity);
        Bundle extras = getIntent().getExtras();
        viewPager = (RecyclerView) findViewById(R.id.view_pager);
        if (extras != null) {
            //filename.add(extras.getString("filename"));
            filename = (ArrayList<String>) getIntent().getSerializableExtra("filename");
            Log.e("filename1111", String.valueOf(filename));
            Uri imageUri = getIntent().getData();
            Log.e("uri", String.valueOf(imageUri), null);
            allfile = new File(String.valueOf(imageUri));
            mainfile.add(allfile);
            images_uri.add(imageUri);
            Log.e("name", allfile.getName());
            Log.e("name33333333333333", mainfile.toString());

        }
        adapter = new DataAdapter(this, images_uri);
        viewPager.setAdapter(adapter);
    }
}

DataAdapter.java

package www.welkinfort.com.whatsappgallery;

import android.content.Context;
import android.net.Uri;
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.ImageView;

import java.util.ArrayList;

/**
 * Created by Admin on 04-Sep-17.
 */

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
    private ArrayList<Uri> file_url;
    private Context context;

    public DataAdapter(Context context,ArrayList<Uri> file_url) {
        this.context = context;
        this.file_url = file_url;
        Log.e("filefggdgdgdgd",file_url.toString());
    }

    @Override
    public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_layout, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        viewHolder.img_android.setImageURI(file_url.get(position));
        //Picasso.with(context).load(file_url.get(i)).resize(120, 60).into(viewHolder.img_android);
    }

    @Override
    public int getItemCount() {
        return file_url.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        ImageView img_android;
        public ViewHolder(View view) {
            super(view);
            img_android = (ImageView)view.findViewById(R.id.img_android);
        }
    }
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <LinearLayout
        android:layout_margin="10dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </android.support.v7.widget.RecyclerView>
    </LinearLayout>
</LinearLayout> 

adpter layouy.java

<LinearLayout xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/img_android" />

</LinearLayout> 

0 个答案:

没有答案