我想將用戶從一個片段重定向到另一個片段,但問題是我有 2 個片段布局在彼此之上。所以我有一個包含回收器視圖的第一個片段,如下所示:當用戶按下回收器視圖中的文本時,我想將用戶重定向到另一個片段。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@ id/nav_admin_seller"
tools:context=".Admin.Seller.AdminSellerFragment">
<TextView
android:id="@ id/tv_date_admin_seller"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_marginTop="8dp"
android:gravity="center"
android:textColor="@color/logo_color"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@ id/rv_admin_seller"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="48dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/tv_date_admin_seller" />
</FrameLayout>
這是配接器的一部分,我在其中實作了 onClickListener
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onBindViewHolder(AdminSellerAdapter.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
AdminSeller item = itemList.get(position);
holder.name.setText(String.valueOf(item.getUsername()));
holder.password.setText(String.valueOf(item.getPassword()));
holder.daliycash.setText(String.valueOf(item.getDaliyCash()));
holder.totalcash.setText(String.valueOf(item.getTotalCash()));
holder.name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("call.myfragment.action");
id = String.valueOf(itemList.get(position).getSellerId()) "/" current_date;
intent.putExtra("id",id);
mCtx.sendBroadcast(intent);
}
});
}
然后我在包含 2 個片段的 MainActivity 添加一個廣播接收器:
BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String id = intent.getStringExtra("id");
Bundle arguments = new Bundle();
arguments.putString( "id" , id);
//arguments.putBoolean("cameFromSeller1",true);
Fragment frag = null;
frag = new AdminSellerMoreInfo();
frag.setArguments(arguments);
//Fragment fragment = new AdminSellerFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.nav_admin_seller, frag);
//ft.detach(fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
}
};
Admin.this.registerReceiver(mBroadcastReceiver, new IntentFilter("call.myfragment.action"));
這是第二個片段的 Java 代碼:
public class AdminSellerMoreInfo extends Fragment {
View root;
private String sellerid;
private List<SellerOutStockItem> itemList1 = new ArrayList<>();
private RecyclerView recyclerView1;
private ADMINSELLERADAPTER_ITEM_QUENTITY adapter1;
public AdminSellerMoreInfo() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
root = inflater.inflate(R.layout.fragment_admin_seller_more_info, container, false);
recyclerView1 = root.findViewById(R.id.rv_admin_seller_items_name_q);
Bundle arguments = getArguments();
if (arguments != null) {
sellerid = arguments.getString("id");
String id1 = sellerid;
}
String[] split = sellerid.split("/");
getitems_name(split[0],split[1]);
return root;
}
}
這是第二個片段的布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@ id/nav_admin_seller2"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.recyclerview.widget.RecyclerView
android:id="@ id/rv_admin_seller_items_name_q"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="1dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</FrameLayout>
uj5u.com熱心網友回復:
在 MainActivity 中廣播接收器的 onReceive() 方法中,您寫道-
ft.replace(R.id.nav_admin_seller, frag);
第一個引數是 id,在這里您應該將根布局的 id 放在 main_activity.xml 中,它是執行片段事務時這些片段的實際容器。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/323365.html
