3

我想要底部表,它在 viewpager 内显示两个片段。我使用了两个 Fragment 和 FragmentPagerAdapter 和 BottomSheetDialogFragment 来构建对话框。

我的问题出在 viewPager 中,第二个片段 recyclerview 不滚动。

我的应用程序显示的图像

Recyclerview 适配器

public class LigneListAdapter extends RecyclerView.Adapter<LigneListAdapter.ListViewHolder> {

Context context;
List<Ligne> lignes = new ArrayList<>();
LayoutInflater inflater;
ListListener listener;

public LigneListAdapter(Context context, List<Ligne> list) {

    this.context = context;
    this.lignes = list;
    //this.listener= (ListListener) context;
    inflater = LayoutInflater.from(context);


}

@Override
public ListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View convertView = inflater.inflate(R.layout.ligne_list_row, parent, false);
    ListViewHolder viewHolder = new ListViewHolder(convertView);
    return viewHolder;
}

@Override
public void onBindViewHolder(ListViewHolder holder, int position) {


    holder.ligne_icon.setTag(position);
    holder.ligne_name.setText(lignes.get(position).getNomLigne());

    holder.ligne_name.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           // listener.nameToChnge(lignes.get((Integer) v.getTag()).getId());


        }
    });

}

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

class ListViewHolder extends RecyclerView.ViewHolder {
    TextView ligne_name;
    ImageView ligne_icon;

    public ListViewHolder(View itemView) {
        super(itemView);

        ligne_name = (TextView) itemView.findViewById(R.id.ligne_name);
        ligne_icon= (ImageView) itemView.findViewById(R.id.ligne_icon);

    }
}
}

我的第一个片段代码(与第二个相同)

public class LigneFragment extends Fragment implements ListListener {

private RecyclerView recyclerView;
private LigneDAO ligneDAO ;
private LigneListAdapter adapter;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // super.onCreateView(inflater,container,savedInstanceState);

    View v =  inflater.inflate(R.layout.ligne_fragment_list_view, null);
    ligneDAO = new LigneDAO(getContext());
    ligneDAO.open();

    recyclerView = (RecyclerView) v.findViewById(R.id.recycler_list_view);
    adapter = new LigneListAdapter(getContext(), ligneDAO.getAllLignes());
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    ligneDAO.close();
    return v;
}

@Override
public void nameToChnge(String name) {

}

@Override
public void getStationSelected(Station station) {

}
}

FragmentPagerAdapter

public class PagerAdapter extends FragmentPagerAdapter {

public PagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {

    Fragment frag=null;
    switch (position){
        case 0:
            frag=new LigneFragment();
            break;
        case 1:
            frag=new StationFragment();
            break;
    }
    return frag;
}

@Override
public int getCount() {
    return 2;
}

@Override
public CharSequence getPageTitle(int position) {
    String title=" ";
    switch (position){
        case 0:
            title="Lignes";
            break;
        case 1:
            title="Stations";
            break;
    }

    return title;
}
}

BottomSheetDialogFragment

public class AlsaBottomDialogFragment   extends BottomSheetDialogFragment {


private static final String TAB_POSITION = "tab_position";

private PagerAdapter adapter;
private  ViewPager viewPager;
private TabLayout tabLayout;

private LinearLayoutManager mLinearLayoutManager;
private AlsaRecyclerAdapter mAdapter;

public AlsaBottomDialogFragment(){

}


private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() {

    @Override
    public void onStateChanged(@NonNull View bottomSheet, int newState) {
        if (newState == BottomSheetBehavior.STATE_HIDDEN) {
            dismiss();
        }
    }

    @Override
    public void onSlide(@NonNull View bottomSheet, float slideOffset) {
    }
};

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View root = inflater.inflate(R.layout.bottom_sheet, container);
    tabLayout = (TabLayout) root.findViewById(R.id.tablayout);
    viewPager = (ViewPager) root.findViewById(R.id.viewpager);

    adapter = new PagerAdapter(getChildFragmentManager());
    viewPager.setAdapter(adapter);
    tabLayout.post(new Runnable() {
        @Override
        public void run() {
            tabLayout.setupWithViewPager(viewPager);
        }
    });
    return root;
}


@Override
public void onViewCreated(View contentView, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(contentView, savedInstanceState);
}

@Override
public void setupDialog(Dialog dialog, int style) {
    super.setupDialog(dialog, style);
    View contentView = View.inflate(getContext(), R.layout.bottom_sheet, null);
    dialog.setContentView(contentView);
    CoordinatorLayout.LayoutParams layoutParams =
            (CoordinatorLayout.LayoutParams) ((View) contentView.getParent()).getLayoutParams();
    CoordinatorLayout.Behavior behavior = layoutParams.getBehavior();
    if (behavior != null && behavior instanceof BottomSheetBehavior) {
        ((BottomSheetBehavior) behavior).setBottomSheetCallback(mBottomSheetBehaviorCallback);
    }

}

}

我不知道问题在哪里,请帮助

4

1 回答 1

0

Try and set the adapter after you set the recyclerview layout. You set the adapter before the layout rather than afterwards. Not sure if it will help or not.

于 2020-04-23T05:49:11.917 回答