android 给我代码 设计一个页面 该页面分为左右两部分 左边是一个纵向的列表包含若干商品名称 右边是fragment 点击左侧商品时会将商品信息传入右侧的fragment并以好看的形式向用户表现出来
这里提供一个简单的实现思路,具体的代码实现需要根据具体的需求进行调整和优化。
- 准备数据
首先需要准备商品数据,可以使用一个类来表示商品,包含商品名称、描述、价格等信息。可以使用一个列表来存储多个商品对象。
- 创建布局文件
可以使用一个LinearLayout来作为页面的根布局,将其分为左右两部分。左边使用一个ListView来显示商品列表,右边使用一个FrameLayout来显示Fragment。
- 创建商品列表
创建一个Adapter来将商品数据显示到ListView中,可以使用默认的ArrayAdapter或自定义一个BaseAdapter。在Adapter中实现getView方法,在其中创建一个TextView来显示商品名称。
- 创建Fragment
创建一个Fragment用来显示商品信息,可以在Fragment中使用一个LinearLayout作为根布局。在Fragment中重写onCreateView方法,在其中创建并返回一个包含商品信息的View。
- 点击商品列表项
在ListView的OnItemClickListener中获取点击的商品对象,使用FragmentManager将Fragment添加到FrameLayout中。在添加Fragment时将商品对象作为参数传递给Fragment,可以使用Bundle来传递参数。
- 在Fragment中显示商品信息
在Fragment的onCreateView方法中获取传递的商品对象,使用一个TextView来显示商品信息。可以根据具体需求来设计商品信息的显示方式,例如使用表格或卡片布局等。
下面是一个简单的示例代码:
MainActivity.java
public class MainActivity extends AppCompatActivity {
private ListView mListView;
private FrameLayout mFrameLayout;
private List<Product> mProductList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化商品列表数据
mProductList = new ArrayList<>();
mProductList.add(new Product("商品1", "描述1", 100));
mProductList.add(new Product("商品2", "描述2", 200));
mProductList.add(new Product("商品3", "描述3", 300));
mProductList.add(new Product("商品4", "描述4", 400));
mProductList.add(new Product("商品5", "描述5", 500));
// 获取ListView和FrameLayout
mListView = findViewById(R.id.list_view);
mFrameLayout = findViewById(R.id.frame_layout);
// 创建商品列表Adapter
ProductAdapter adapter = new ProductAdapter(this, mProductList);
mListView.setAdapter(adapter);
// 设置ListView的点击事件
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// 获取点击的商品对象
Product product = mProductList.get(position);
// 创建商品信息Fragment并传递参数
ProductFragment fragment = new ProductFragment();
Bundle args = new Bundle();
args.putString("name", product.getName());
args.putString("description", product.getDescription());
args.putInt("price", product.getPrice());
fragment.setArguments(args);
// 将Fragment添加到FrameLayout中
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_layout, fragment)
.commit();
}
});
}
}
ProductAdapter.java
public class ProductAdapter extends BaseAdapter {
private Context mContext;
private List<Product> mProductList;
public ProductAdapter(Context context, List<Product> productList) {
mContext = context;
mProductList = productList;
}
@Override
public int getCount() {
return mProductList.size();
}
@Override
public Object getItem(int position) {
return mProductList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = new TextView(mContext);
Product product = mProductList.get(position);
textView.setText(product.getName());
return textView;
}
}
ProductFragment.java
public class ProductFragment extends Fragment {
private TextView mNameTextView;
private TextView mDescriptionTextView;
private TextView mPriceTextView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_product, container, false);
// 获取商品信息TextView
mNameTextView = view.findViewById(R.id.name_text_view);
mDescriptionTextView = view.findViewById(R.id.description_text_view);
mPriceTextView = view.findViewById(R.id.price_text_view);
// 获取传递的商品信息参数
Bundle args = getArguments();
if (args != null) {
String name = args.getString("name");
String description = args.getString("description");
int price = args.getInt("price");
// 显示商品信息
mNameTextView.setText(name);
mDescriptionTextView.setText(description);
mPriceTextView.setText(String.format(Locale.getDefault(), "价格:%d元", price));
}
return view;
}
}
activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ListView
android:id="@+id/list_view"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<FrameLayout
android:id="@+id/frame_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"/>
</LinearLayout>
fragment_product.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/name_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/description_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:textSize="16sp"/>
<TextView
android:id="@+id/price_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:textSize="16sp"
android:textStyle="bold"/>
</LinearLayout>
``
原文地址: http://www.cveoy.top/t/topic/djs4 著作权归作者所有。请勿转载和采集!