我有一个片段实现的列表视图。当我单击一个项目时,我想过渡到详细信息片段,我想在另一片段中显示详细信息,在该片段中,我对该图像有一个imageview,而名称,价格和详细信息则为3个textview。我尝试了几种变体,但它们没有成功。非常感谢
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_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
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;
}
}
答案 0 :(得分:0)
嗨,您可以使用以下代码。
首先将一个接口创建为适配器类和“第一片段”实现接口。
接口具有一种回调方法。因此,当用户单击列表项时,则使用下面的方法
@override callback(){
}
进入第一片段。
在过渡代码下方的内部回调方法。
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();