简体   繁体   English

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

[英]How can i make transaction from a listview adapter from a fragment to another fragment

I have a listview that I implemented in a fragment.我有一个在片段中实现的列表视图。 I want to make the transition to the details fragment at the moment where I click on one item and I want to display the details in the other fragment where i have one imageview for that image and 3 textview for name,price and details.我想在单击一个项目的那一刻转换到详细信息片段,我想在另一个片段中显示详细信息,其中我有一个 imageview 用于该图像,3 textview 用于名称、价格和详细信息。 I have tried several variants, but they do not succeed.我尝试了几种变体,但都没有成功。 thanks a lot多谢

items.java项目.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项目适配器.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 list_item.xml

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

    <ImageView
        android:id="@+id/icon_item"
        android:layout_width="100dp"
        android:layout_height="80dp" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical"
        android:paddingLeft="16dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Name"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/name_item"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Price"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/price_item"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical"

        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Details"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/details_item"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

</LinearLayout>


beds_fragment.xml


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

bedsFragment.java床Fragment.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详细信息Fragment.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;
  }
}

Hi you can use below code.您好,您可以使用以下代码。

first of all one interface create into adapter class and First Fragment implement interface.首先一个接口创建到适配器 class 和 First Fragment 实现接口。

interface have one callback method.接口有一个回调方法。 so when user click list item,then below method所以当用户点击列表项时,下面的方法

@override callback(){ @覆盖回调(){

} }

into first-fragment.进入第一个片段。

inside callback method below transition code right.在转换代码右下方的回调方法中。

 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 SQL 数据库在 Fragment 上创建 Adapter ListView? - How can I make an Adapter ListView on a Fragment using an SQL database? 如何从另一个片段中对适配器进行排序 - How to sort adapter from another fragment 如何在片段内为Listview创建工作适配器 - How to make work an Adapter to Listview inside a fragment 从片段更新ListView适配器内的TextView - Updating TextView inside ListView Adapter from a Fragment 如何从主Activity中刷新片段中的ListView? - How can I refresh a ListView in a Fragment from the main Activity? 如何从适配器调用片段 - How to call fragment from adapter 我可以通过捆绑从适配器发送数据(当单击适配器中自定义布局的视图按钮时)到另一个片段? - can I send data from adapter by bundle( when click on view button from custom layout in adapter) to another fragment? 我无法将带有捆绑的ArrayList从一个片段放到另一个片段中 - I can't put a ArrayList with a bundle from a fragment into another fragment 从另一个片段重置RecyclerView适配器 - Reset RecyclerView adapter from another Fragment 从片段的适配器(A)导航到另一个片段(B)时无法解析上下文或应用程序 - Can't resolve the Context or Application while navigating from Adapter of a fragment(A) to another Fragment (B)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM