gpt4 book ai didi

java - RecyclerView 不会用项目单击上的 fragment 替换详细信息 Pane

转载 作者:行者123 更新时间:2023-12-02 10:39:46 26 4
gpt4 key购买 nike

每次我在平板电脑上单击回收器 View 项目时,它都会打开一个 Activity ,而不是用 fragment 替换详细信息 Pane 。

以下代码行是我用来检测详细信息 Pane 是否存在的代码:

mTwoPane = Objects.requireNonNull(getActivity()).findViewById(R.id.detail_container) != null;

关于放置这行代码的正确位置有什么想法吗?

Activity XML

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

<android.widget.Toolbar
android:id="@+id/masterToolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
>


<LinearLayout
android:id="@+id/singleline_text_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="vertical">

<TextView
android:id="@+id/md_toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@android:style/TextAppearance.Material.Widget.ActionBar.Title"/>
</LinearLayout>

</android.widget.Toolbar>


<RelativeLayout
android:id="@+id/master_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

</LinearLayout>

sw600dp Activity XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.widget.Toolbar
android:id="@+id/masterToolbar"
android:layout_width="0dp"
android:layout_height="?actionBarSize"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@+id/detailBackgroundToolbar"
app:layout_constraintHorizontal_weight="2"
app:layout_constraintTop_toTopOf="parent">


<LinearLayout
android:id="@+id/singleline_text_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="vertical">

<TextView
android:id="@+id/md_toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@android:style/TextAppearance.Material.Widget.ActionBar.Title"/>
</LinearLayout>

</android.widget.Toolbar>


<RelativeLayout
android:id="@+id/master_container"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintHorizontal_weight="2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@+id/divider"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/masterToolbar" />

<View
android:id="@+id/divider"
android:layout_width="1dp"
android:layout_height="0dp"
android:background="?attr/dividerColor"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/masterToolbar"
app:layout_constraintTop_toBottomOf="@+id/masterToolbar" />

<android.widget.Toolbar
android:id="@+id/detailBackgroundToolbar"
android:layout_width="0dp"
android:layout_height="?actionBarSize"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_weight="3"
app:layout_constraintStart_toEndOf="@+id/masterToolbar"
app:layout_constraintTop_toTopOf="parent" />

<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="4dp"
app:cardCornerRadius="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/divider"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.5">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<include layout="@layout/toolbar_dualline"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<FrameLayout
android:id="@+id/detail_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>

</android.support.v7.widget.CardView>

<View
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/divider"
app:layout_constraintTop_toBottomOf="@+id/detailBackgroundToolbar" />

</android.support.constraint.ConstraintLayout>

fragment 类

public class MyFragment extends Fragment {

public MyFragment() {}

List<Product> wcList;

RecyclerView mRecyclerView;

/**
* Whether or not the activity is in two-pane mode, i.e. running on a tablet device.
*/
public boolean mTwoPane;

public static MyFragment newInstance() {
return new MyFragment();
}

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_main, container, false);
mTwoPane = Objects.requireNonNull(getActivity()).findViewById(R.id.detail_container) != null;

mRecyclerView = view.findViewById(R.id.recyclerView_list);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
mRecyclerView.addItemDecoration(new DividerItemDecoration(Objects.requireNonNull(getContext()), LinearLayout.VERTICAL));

myList = new ArrayList<>();

String[] items = getResources().getStringArray(R.array.product_names);
String[] itemDescriptions = getResources().getStringArray(R.array.product_descriptions);
for (int n = 0; n < items.length; n++){
Product desserts = new Product();
desserts.setProductName(items[n]);
wdessertsc.setProductDescriptions(itemDescriptions[n]);
myList.add(desserts);
}

MyListAdapter listAdapter = new MyListAdapter(getActivity(), myList);

mRecyclerView.setAdapter(listAdapter);

return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
mTwoPane = Objects.requireNonNull(getActivity()).findViewById(R.id.detail_container) != null;

super.onActivityCreated(savedInstanceState);
}
}

适配器类

    public class MyListAdapter extends RecyclerView.Adapter<MyListAdapter.MyViewHolder> {
public boolean mTwoPane;

private Context mCtx;

private List<Product> myList;

public MyListAdapter(Context mCtx, List<Product> myList) {
this.mCtx = mCtx;
this.myList = myList;
}

@NonNull
@Override
public MyListAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.listitem_dualline, parent,false);
return new MyListAdapter.MyViewHolder(view);
}

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

final Product product = myList.get(holder.getAdapterPosition());

holder.textviewTitle.setText(product.getProductName());
holder.textviewSubtitle.setText(product.getPRoductDescription());


holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mTwoPane) {
Fragment newFragment;
if (product.getStationName().equals(v.getResources().getString(R.string.product_1))) {
newFragment = new FragmentProduct1();
} else {
newFragment = new FragmentProdcut2();
}
MyActivity activity = (MyActivity) v.getContext();
FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.detail_container, newFragment);
transaction.commit();
} else {
Intent intent;
if (product.getStationName().equals(v.getResources().getString(R.string.product_1))) {
intent = new Intent(v.getContext(), Product1Activity.class);
} else {
intent = new Intent(v.getContext(), Product2Activity.class);
}
mCtx.startActivity(intent);
}
}
});
}

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

class MyViewHolder extends RecyclerView.ViewHolder {
RelativeLayout relativeLayout;
TextView textviewTitle, textviewSubtitle;

StationViewHolder(View itemView) {
super(itemView);

mTwoPane = itemView.findViewById(R.id.detail_container) != null;

relativeLayout = itemView.findViewById(R.id.listitem_relativelayout);
textviewTitle = itemView.findViewById(R.id.listitem_title);
textviewSubtitle = itemView.findViewById(R.id.listitem_subtitle);
}
}
}

最佳答案

View 持有者中的这一行 mTwoPane = itemView.findViewById(R.id.detail_container) != null; 将始终为 false

为什么?

因为,您的 detail_container 不是 View 持有者项目容器的一部分,因此 itemView 将始终返回 null 供您查看。相反,将 boolean 标志从 fragment 传递到适配器!

关于java - RecyclerView 不会用项目单击上的 fragment 替换详细信息 Pane ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53009330/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com