我如何从一个片段到另一个片段的列表视图适配器进行事务

Posted

技术标签:

【中文标题】我如何从一个片段到另一个片段的列表视图适配器进行事务【英文标题】:How can i make transaction from a listview adapter from a fragment to another fragment 【发布时间】:2020-02-20 01:13:11 【问题描述】:

我有一个在片段中实现的列表视图。我想在单击一个项目的那一刻转换到详细信息片段,并且我想在另一个片段中显示详细信息,其中我有该图像的一个图像视图和名称、价格和详细信息的 3 个文本视图。我尝试了几种变体,但都没有成功。非常感谢

items.java

public class items 

    // Name of the object
    private String lName;

    // Costs
    private String lPrice;

    // Details
    private String lDetails;

    // Image resource id
    private int lImageId;

    // Constructor
    public items(String ObjectName,String ObjectPrice,int ImageResourceId,String DetailsItem)
        lName=ObjectName;
        lPrice=ObjectPrice;
        lImageId=ImageResourceId;
        lDetails=DetailsItem;

    

    // Getters
    public String getlName()  return lName; 
    public String getlPrice() return lPrice; 
    public int getlImageId() return lImageId; 
    public String getlDetails()  return lDetails; 

    

itemsAdapter.java

public class itemsAdapter extends ArrayAdapter<items>

    private static final String LOG_TAG = itemsAdapter.class.getSimpleName();

    /**
     * This is our own custom constructor (it doesn't mirror a superclass constructor).
     * The context is used to inflate the layout file, and the list is the data we want
     * to populate into the lists.
     */
    public itemsAdapter(Activity context, ArrayList<items> item)
        super(context,0,item);
    

    //Provides a view for an AdapterView (ListView, GridView, etc.)
    public View getView(int position, View convertView, ViewGroup parent)
        // Check if the existing view is being reused, otherwise inflate the view
        View listItemView = convertView;
        if(listItemView == null) 
            listItemView = LayoutInflater.from(getContext()).inflate(
                    R.layout.list_item, parent, false);
        

        // Get the object located at this posiiton in the list
        items currentItems=getItem(position);

        // Find the TextView in the list_item.xml layout with the id name_items
        TextView nameTextView=(TextView)listItemView.findViewById(R.id.name_item);

        // Get the name from the current items object and set this text on the name TextView
        nameTextView.setText(currentItems.getlName());

        // Find the TextView in the list_item.xml layout with the id price_items
        TextView priceTextView=(TextView)listItemView.findViewById(R.id.price_item);

        // Get the price from the current items object and set this text on the price TextView
        priceTextView.setText(currentItems.getlPrice());

        // Find the ImageView in the list_item.xml layout with the id icon_item
        ImageView iconImageView=(ImageView)listItemView.findViewById(R.id.icon_item);

        // Get the image from the current items object and set this image on the image view
        iconImageView.setImageResource(currentItems.getlImageId());


        // Find the TextView in the list_item.xml layout with the id details_item
        TextView detailsView=(TextView)listItemView.findViewById(R.id.details_item);

        // Get the details from the current items object and set this text on the details TextView
        detailsView.setText(currentItems.getlDetails());

        return listItemView;
    


list_item.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:orientation="horizontal"
    android:padding="16dp">

    <ImageView
        android:id="@+id/icon_item"
        android:layout_
        android:layout_ />

    <LinearLayout
        android:layout_
        android:layout_
        android:layout_weight="1"
        android:orientation="vertical"
        android:paddingLeft="16dp">

        <TextView
            android:layout_
            android:layout_
            android:text="Name"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/name_item"
            android:layout_
            android:layout_ />

        <TextView
            android:layout_
            android:layout_
            android:text="Price"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/price_item"
            android:layout_
            android:layout_ />

    </LinearLayout>

    <LinearLayout
        android:layout_
        android:layout_
        android:layout_weight="1"
        android:orientation="vertical"

        >

        <TextView
            android:layout_
            android:layout_
            android:text="Details"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/details_item"
            android:layout_
            android:layout_ />

    </LinearLayout>

</LinearLayout>


beds_fragment.xml


    <ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listview_beds"
    android:layout_
    android:layout_
    android:layout_gravity="bottom"
    android:background="#e3e3e3"
    />

bedsFragment.java

public class bedsFragment extends Fragment 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 

        View view = inflater.inflate(R.layout.beds_fragment, container, false);


        // Create an ArrayList of items objects
        ArrayList<items> beds=new ArrayList<items>();
        beds.add(new items("Bed1","23$",R.drawable.bed1,"Beds for sleeping well, best buy if you want to sleep."));
        beds.add(new items("Bed2","43$",R.drawable.bed2,"Beds for sleeping well, best buy if you want to sleep."));
        beds.add(new items("Bed3","54$",R.drawable.bed3,"Beds for sleeping well, best buy if you want to sleep."));
        beds.add(new items("Bed4","34$",R.drawable.bed4,"Beds for sleeping well, best buy if you want to sleep."));
        beds.add(new items("Bed5","65$",R.drawable.bed5,"Beds for sleeping well, best buy if you want to sleep."));

        // Create an @link itemsAdapter, whose data source is a list of @items. The adapter
        // know how to create a list item views for each item in the list

        itemsAdapter itAdapter=new itemsAdapter(this.getActivity(),beds);

        // Get a reference to the ListView, and attach the adapter to the listView
        ListView listView=(ListView)view.findViewById(R.id.listview_beds);
        listView.setAdapter(itAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) 

                FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.listview_beds, new storeFragment());
                fragmentTransaction.addToBackStack(null).commit();
            
        );


        return view;
    

    

detailsFragment.java

public class detailsFragment extends Fragment 
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 

    View view = inflater.inflate(R.layout.details_fragment, container, false);
    return view;
  

【问题讨论】:

【参考方案1】:

您好,您可以使用以下代码。

首先一个接口创建成适配器类和First Fragment实现接口。

接口有一个回调方法。所以当用户点击列表项时,下面的方法

@覆盖 回调()

进入第一个片段。

转换代码右下方的内部回调方法。

 FragmentTransaction fragmentTransaction = getSupportFragmentManager()
                    .beginTransaction();
    Fragment profileFragment = new MovieDetailFragment();//the fragment you want to show
    profileFragment.setArguments(bundle);
    fragmentTransaction
        .replace(R.id.content_frame, profileFragment);//R.id.content_frame is the layout you want to replace
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();

【讨论】:

公共类 itemsAdapter 扩展 ArrayAdapter public interface transitionInterface void callback(); 这是接口,我实现了,“实现 itemsAdapter.transitionInterface public void callback() FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); Fragment profileFragment = new detailsFragment();//你要展示的片段 //profileFragment.setArguments(bundle); fragmentTransaction.replace(R.id.beds_category, profileFragment); fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() @Override public void onItemClick(AdapterView> parent, View view, int position, long id) callback(); ); 当我点击时,我的应用程序崩溃

以上是关于我如何从一个片段到另一个片段的列表视图适配器进行事务的主要内容,如果未能解决你的问题,请参考以下文章

从适配器访问片段

如何从使用视图寻呼机创建的选项卡片段转移到另一个片段

如何从片段内的列表视图打开链接网址?

Android,从其他片段返回的空列表视图

如何将可扩展列表视图的选定子视图数据从片段发送到父活动?

从活动更新视图页面中片段中的列表视图