如何在fragmentlerview片段中对recyclerviewadapter进行充气?

时间:2018-01-22 07:11:16

标签: android android-studio android-fragments android-recyclerview layout-inflater

我的SavedActivity.java:

public class SavedActivity extends Fragment {
private static final String MyApp = "/MyApp";
RecyclerView RecyclerViewMediaList;
LinearLayoutManager mLinearLayoutManager;
public SavedActivity() {
    // 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 inflatedView = inflater.inflate(R.layout.activity_saved, container, false);
    RecyclerViewMediaList = (RecyclerView)container.findViewById(R.id.recycler_View);
adaptRecyclerView();
return inflatedView;
 private ArrayList<File> getListFiles(File parentDir) {
    ArrayList<File> inFiles = new ArrayList<>();
    File[] files;
    files = parentDir.listFiles();
    if (files != null) {
        for (File file : files) {
            if (file.getName().endsWith(".jpg"))
            {
                if (!inFiles.contains(file))
                    inFiles.add(file);
            }
        }
    }
    return inFiles;
}

public void adaptRecyclerView(){

    mLinearLayoutManager = new LinearLayoutManager(getContext());
    RecyclerViewMediaList.setLayoutManager(mLinearLayoutManager);
    RecyclerViewAdapterSaved recyclerViewMediaAdapter = new RecyclerViewAdapterSaved(this.getListFiles(new File(Environment.getExternalStorageDirectory().toString()+ MyApp)), getActivity());
    RecyclerViewMediaList.setAdapter(recyclerViewMediaAdapter);
}

我的RecyclerViewAdapterSaved.java:

public class RecyclerViewAdapterSaved extends RecyclerView.Adapter<RecyclerViewAdapterSaved.FileHolder> {
private ArrayList<File> filesList;
Activity activity;


public RecyclerViewAdapterSaved(ArrayList<File> filesList, Activity activity) 
{
    this.filesList = filesList;
    this.activity = activity;

}

@Override
public RecyclerViewAdapterSaved.FileHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View inflatedView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_adapter_saved, parent, true);
    return new FileHolder(inflatedView);
}
@Override
public void onBindViewHolder(final RecyclerViewAdapterSaved.FileHolder holder, final int position) {
    final File currentFile = filesList.get(position);
showImages(currentFile,holder,position);
public void showImages(final File currentFile, final RecyclerViewAdapterSaved.FileHolder holder, final int position){
    holder.cardviewImageViewSaved.setVisibility(View.VISIBLE);
    Bitmap myBitmap = BitmapFactory.decodeFile(currentFile.getAbsolutePath());
    holder.imageViewSaved.setImageBitmap(myBitmap);
}
}

我试图在recycler_view_adapter_saved.xml中提供的activity_saved.xml中试用<button id="sendContact" type="submit" name="sendContact" value="sendContact_data">Submit</button> ,当我尝试这样做时代码运行良好但在RecyclerView中没有视图。上面的代码是正确的膨胀布局?。还有其他方法吗?。更改查看inflatedView = LayoutInflater.from(parent.getContext())。inflate(R.layout.recycler_view_adapter_saved,parent,false); 它在另一个片段中显示输出。

2 个答案:

答案 0 :(得分:0)

试试这个,您必须在fragment方法

上返回将在此onCreateView上曝光的视图
public class SavedActivity extends Fragment {
private static final String MyApp = "/MyApp";
RecyclerView RecyclerViewMediaList;
LinearLayoutManager mLinearLayoutManager;
public SavedActivity() {
    // 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 inflatedView = inflater.inflate(R.layout.activity_saved, container, false);
    RecyclerViewMediaList = (RecyclerView)container.findViewById(R.id.recycler_View);
    adaptRecyclerView();

    return inflatedView;
}

public void adaptRecyclerView(){

    mLinearLayoutManager = new LinearLayoutManager(getContext());
    RecyclerViewMediaList.setLayoutManager(mLinearLayoutManager);
    RecyclerViewAdapterSaved recyclerViewMediaAdapter = new RecyclerViewAdapterSaved(this.getListFiles(new File(Environment.getExternalStorageDirectory().toString()+ MyApp)), getActivity());
    RecyclerViewMediaList.setAdapter(recyclerViewMediaAdapter);
}

private ArrayList<File> getListFiles(File parentDir) {
    ArrayList<File> inFiles = new ArrayList<>();
    File[] files;
    files = parentDir.listFiles();
    if (files != null) {
        for (File file : files) {
            if (file.getName().endsWith(".jpg"))
            {
                if (!inFiles.contains(file))
                    inFiles.add(file);
            }
        }
    }
 }

答案 1 :(得分:0)

在Viewholder中尝试此操作

    View inflatedView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_adapter_saved, parent, false);