有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在运行时将数据添加到片段中的RecyclerView,在该片段中,数据是在单独的片段中创建的

我创建了一个包含3个不同片段的应用程序。一个片段有一个按钮,可以将数据添加到一个单独片段中的recyclerview,但是它不能正常工作。我尝试过使用FragmentManager,这会导致空指针异常;我尝试过在适配器和数据类中使用notifyDataChanged,这不会更新片段中的数据;我尝试过重置viewholder,这也会导致空指针异常。我的点子快用完了

我的适配器在这里:

package com.example.dogwalk;

import 安卓.content.Context;
import 安卓.support.annotation.NonNull;
import 安卓.support.v7.widget.RecyclerView;
import 安卓.util.Log;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.widget.ImageView;
import 安卓.widget.RelativeLayout;
import 安卓.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

public class DogInfoAdapter extends RecyclerView.Adapter<DogInfoAdapter.ViewHolder>
{
    private static final String TAG = "DogInfoAdapter";

    private ArrayList<String> mDogType;
    private ArrayList<String> mImages;
    private ArrayList<String> mDogInfo;
    private Context mContext;

    public DogInfoAdapter(ArrayList<String> mDogType, ArrayList<String> mImages,
                          ArrayList<String> mDogInfo, Context mContext)
    {
        this.mDogType = mDogType;
        this.mImages = mImages;
        this.mDogInfo = mDogInfo;
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i)
    {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem_doginfo, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position)
    {
        Log.d(TAG, "onBindViewHolder: called.");

        Glide.with(mContext)
                .asBitmap()
                .load(mImages.get(position))
                .into(holder.imageView);

        holder.dogType.setText(mDogType.get(position));

        holder.dogInfo.setText(mDogInfo.get(position));
    }

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


    public class ViewHolder extends RecyclerView.ViewHolder
    {

        ImageView imageView;
        TextView dogType;
        TextView dogInfo;
        RelativeLayout parentLayout;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.image);
            dogType = itemView.findViewById(R.id.dogType);
            dogInfo = itemView.findViewById(R.id.dogInfo);
            parentLayout = itemView.findViewById(R.id.parent_layout);

        }
    }

}

和我的数据类

注意,我在底部的“AddData”方法只是测试不同的解决方案,计划是在一个单独的片段中动态添加数据

package com.example.dogwalk;
import 安卓.graphics.Bitmap;
import 安卓.support.annotation.NonNull;
import 安卓.support.annotation.Nullable;
import 安卓.support.v4.app.Fragment;
import 安卓.os.Bundle;
import 安卓.support.v4.app.FragmentManager;
import 安卓.support.v4.app.FragmentTransaction;
import 安卓.support.v7.widget.DividerItemDecoration;
import 安卓.support.v7.widget.LinearLayoutManager;
import 安卓.support.v7.widget.RecyclerView;
import 安卓.util.Log;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.view.ViewGroup;

import java.util.ArrayList;


public class DogInfo extends Fragment 
{
    private static final String TAG = "DogInfo";
    private ArrayList<String> mNames = new ArrayList<>();
    private  ArrayList<String> mImageUrls = new ArrayList<>();
    private ArrayList<String> mDogInfo = new ArrayList<>();
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private View mView;

    public static DogInfo newInstance() {
        DogInfo fragment = new DogInfo();
        return fragment;
    }


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        initImageBitmaps();
        View mView = inflater.inflate(R.layout.doginfo_layout, container, false);
        mRecyclerView = mView.findViewById(R.id.recyclerview);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(mRecyclerView.getContext(), DividerItemDecoration.VERTICAL));
        mAdapter = new DogInfoAdapter(mNames, mImageUrls, mDogInfo, mView.getContext());
        mLayoutManager = new LinearLayoutManager(getContext());
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mAdapter);

        return mView;
    }

    private void initImageBitmaps()
    {
        Log.d(TAG, "initImageBitmaps: Preparing Bitmaps.");

        mImageUrls.add("https://upload.wikimedia.org/wikipedia/commons/d/de/Smooth_Dachshund_red_and_tan_portrait.jpg");
        mNames.add("Dachshund");
        mDogInfo.add("You should be giving your dog a 45-60 minute walk a day (maybe split in two lots). Once adult, your Dachshund will take any amount of exercise you care to give. ");


        mImageUrls.add("https://upload.wikimedia.org/wikipedia/commons/2/26/YellowLabradorLooking_new.jpg");
        mNames.add("Labrador");
        mDogInfo.add("A normal, healthy adult Labrador Retriever will need 1 hour of exercise every day. The more relaxed Labs just 45 minutes per day, the more energetic 1.5 hours+.");

        mImageUrls.add("https://upload.wikimedia.org/wikipedia/commons/2/2c/West_Highland_White_Terrier_Krakow.jpg");
        mNames.add("West Highland Terrier");
        mDogInfo.add("The length of the walk is only limited by your time and energy as a West Highland Terrier can go all day long. Ideally, the minimum should be 30 to 60 minutes daily. This can be just once a day, but ideally split over two walks a day");

        mImageUrls.add("https://upload.wikimedia.org/wikipedia/commons/f/fa/Buster_the_red_grizzle_border_terrier_%282006%29.jpg");
        mNames.add("Border Terrier");
        mDogInfo.add("The length of the walk is only limited by your time and energy as a Border Terrier can go all day long. Ideally, the minimum should be 45 to 60 minutes daily. This can be just once a day, but two walks a day would be better.");


        mImageUrls.add("https://upload.wikimedia.org/wikipedia/commons/6/6d/Alaskan_malamut_465.jpg");
        mNames.add("Alaskan Malamut");
        mDogInfo.add("A couple of daily walks (one is at least 45 minutes) or a shorter walk and a vigorous play session (30 minutes or more). Malamutes can be stubborn, but they are also eager to please. Training early on will make play and exercise and life in general easier.");


        mImageUrls.add("https://upload.wikimedia.org/wikipedia/commons/6/6c/Border_Collie_rojo_mirlo_tricolor_%28Birdy_de_los_Baganicos%29.jpg");
        mNames.add("Border Collie");
        mDogInfo.add("Although as much as they possibly can should be the right answer, but at least 20-30 minutes of running everyday is ideal. However, undoubtedly your Border Collie would definitely prefer a lot more.");


//        mImageUrls.add("https://en.wikipedia.org/wiki/English_Springer_Spaniel#/media/File:%C3%89pagneul_Anglais.jpg");
//        mNames.add("Springer Spaniel");

        mImageUrls.add("https://upload.wikimedia.org/wikipedia/commons/d/de/Beagle_Upsy.jpg");
        mNames.add("Beagle");
        mDogInfo.add("You should walk your Beagle for a minimum of 20 minutes, ideally 30, and some Beagles may need up to 40 minutes.");

//
//        mImageUrls.add("https://en.wikipedia.org/wiki/Shiba_Inu#/media/File:Shiba_inu_taiki.jpg");
//        mNames.add("Shiba Inu");
//
//        mImageUrls.add("https://en.wikipedia.org/wiki/Samoyed_dog#/media/File:Samojed00.jpg");
//        mNames.add("Samoyed");
    }

    public void AddData()
    {

        mNames.add("Test");
        mImageUrls.add("Test");
        mDogInfo.add("Test");
        FragmentManager manager = getActivity().getSupportFragmentManager();
        FragmentTransaction ft = manager.beginTransaction();
        Fragment newFragment = this;
        this.onDestroy();
        ft.remove(this);
        ft.replace(getView().getId(),newFragment);
        //container is the ViewGroup of current fragment
        ft.addToBackStack(null);
        ft.commit();
    }



}

共 (1) 个答案

  1. # 1 楼答案

    您有一个承载两个片段的活动:

    • 包含一个recyclerView的FragmentB
    • FragmentA将项目动态添加到FragmentB的recyclerView

    因此,需要的是FragmentA引用FragmentB来修改它。(如果我错了,请纠正我)

    有几种方法可以实现这一点:

    一种方法是让片段通过其活动相互对话,如下所示:

    1. 每当活动想要修改recyclerView中的项目时,它都想要收听FragmentA;这可以通过创建由活动实现的接口@FragmentA来实现
    2. 当FragmentA触发活动时,它必须将请求发送到 FragmentB的公共方法,以修改recyclerView

    对于代码片段,您可以参考我的答案here;不同之处在于FragmentA更改了TextView的内容,而不是RecyclerView的内容。。。如果您需要RecyclerView的另一个片段,请告诉我