如何解决如何在android studio的Fragment中的Listview中创建自定义行
我创建了一个 XML 文件以在列表视图中显示为列表项,但无法在片段中实现它。任何人都可以帮我解决这个问题。下面给出了我的代码的详细信息。
这是我用于列表视图的 row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/goal_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Item one"
android:textSize="18sp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:textStyle="bold" />
<TextView
android:id="@+id/goal_distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/goal_name"
android:layout_marginLeft="20dp"
android:layout_marginTop="-8dp"
android:layout_marginBottom="10dp"
android:text="summary 1"
android:textSize="10sp" />
<TextView
android:id="@+id/goal_steps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/goal_name"
android:layout_marginStart="13dp"
android:layout_marginLeft="60dp"
android:layout_marginTop="-8dp"
android:layout_marginBottom="10dp"
android:layout_toEndOf="@+id/goal_distance"
android:text="summary 2"
android:textSize="10sp" />
<Button
android:id="@+id/goalActiveBtn"
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_marginStart="130dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="12dp"
android:layout_marginRight="50dp"
android:layout_toEndOf="@+id/goal_name"
android:text="Activate"
android:textColor="@android:color/black"
app:iconTint="#2196F3" />
</RelativeLayout>
这是列表视图 fragment_goal.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".GoalFragment">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:listitem="@android:layout/simple_list_item_1"
android:id="@+id/goal_list_id"
android:padding="16dip"
android:clipToPadding="false"
android:choiceMode="singleChoice"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
这是我想用自定义行实现列表视图的片段
package com.xyz.test;
import android.content.Context;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class ActivityFragment extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
ListView listView;
public ActivityFragment() {
// Required empty public constructor
}
public static ActivityFragment newInstance(String param1,String param2) {
ActivityFragment fragment = new GoalFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1,param1);
args.putString(ARG_PARAM2,param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setTitle("GOAL");
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_goal,container,false);
listView = (ListView)rootView.findViewById(R.id.goal_list_id);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,this.itemNames());
listView.setAdapter(arrayAdapter);
return rootView;
}
private ArrayList itemNames(){
ArrayList<String> item_names = new ArrayList<>();
item_names.add("Item 1 - (1ef,$16000)");
item_names.add("Item 2 - (2ef,$20000)");
item_names.add("Item 3 - (3ef,$36000)");
item_names.add("Item 4 - (4ef,$5000)");
item_names.add("Item 5 - (5ef,$7000)");
return item_names;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。