Android - 对整个 ListView 使用 SwipeView
Android - Using SwipeView for an entire ListView
我看过几个在 ListView 上实现的 SwipeView 示例 (here),但这些是针对列表中的个别项目而不是针对 ListView 本身。
在我的应用程序中,一个意图从一个 Listview 项目开始,然后将您发送到有问题的 Listview。从那里,我想实现某种功能,允许我从一个 ListView 滑动到另一个。在这种情况下,一直用意图向下钻取不会像 SwipeView 那样提供 user-friendly 体验。
如果我没有解释清楚,请告诉我。如果我必须创建额外的 classes 和 XML 布局文件来执行此操作,那就这样吧。
CustomListAdapter.java(保存ListView代码的class)
public class CustomListAdapter extends ArrayAdapter<String> {
private Context mContext;
private int id;
private ArrayList<String> callData;
private String[] headers = {"Number", "Customer Number", "Name", "Bill to Customer Number", "Bill to Name",
"Order Date", "Order Time", "Response Date", "Response Time", "Fix by Date", "Fix by Time",
"Responded Date", "Responded Time", "Fixed Date", "Fixed Time", "Your Reference",
"Description", "Transaction Status", "Ship to Code", "Ship to Name", "Ship to Address",
"Ship to Address 2", "Ship to City", "Ship to County", "Ship to Postcode", "Contact Name",
"Phone Number", "Note", "Sources"};
public CustomListAdapter(Context context, int textViewResourceId, ArrayList<String> callData) {
super(context, textViewResourceId, callData);
this.callData = callData;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.list_service_order, null);
}
String test = callData.get(position);
if (test != null) {
TextView customerNoLabel = (TextView) view.findViewById(R.id.name_label);
TextView customerNoData = (TextView) view.findViewById(R.id.listed_data);
if (customerNoLabel != null) {
customerNoLabel.setText(headers[position]);
customerNoData.setText(callData.get(position));
}
}
return view;
}
}
CallActivity2.java(实现 CustomListAdapter 代码的 class,我想用它滑动到具有不同标题和值的两个不同列表视图)
public class CallActivity2 extends ListActivity {
private ArrayList<String> individualCallData;
// private ArrayAdapter<String> callDataAdapter;
private CustomListAdapter callDataAdapter;
private ListView callDataView;
private Runnable viewParts;
private TextView serviceOrderNo;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.call_display);
Intent intent = getIntent();
individualCallData = intent.getStringArrayListExtra("Call");
// Instantiate CustomListAdapter class
callDataView = (ListView) findViewById(android.R.id.list);
callDataAdapter = new CustomListAdapter(this, R.layout.list_service_order, individualCallData);
setListAdapter(callDataAdapter);
viewParts = new Runnable(){
public void run(){
handler.sendEmptyMessage(0);
}
};
// here we call the thread we just defined - it is sent to the handler below.
Thread thread = new Thread(null, viewParts, "MagentoBackground");
thread.start();
}
private Handler handler = new Handler() {
public void handleMessage(Message msg)
{
// create some objects
// here is where you could also request data from a server
// and then create objects from that data.
//individualCallData.add("Test");
callDataAdapter = new CustomListAdapter(CallActivity2.this, R.layout.list_service_order, individualCallData);
// display the list.
setListAdapter(callDataAdapter);
}
};
}
我推荐使用像这样的库https://github.com/xenione/SwipeLayout
并且不要尝试自己做。并且请避免使用 ListView,而是使用 RecyclerView。
我认为您正在寻找 ViewPager
。在这种情况下,您不需要创建更多 xml 个文件(除非您希望每个列表中的列表项看起来不同),但您将需要创建一些 Fragments
。这样,你可以为每个列表创建一个新的Fragment
,ViewPager
会处理滑动手势在两者之间来回切换。
相反,我决定摆脱 ListView 并改用 ScrollView。我可以做的事情更加灵活。此外,我也可以滚动和滑动。 Listview 不会那样做,也不是项目点击。不过还是感谢两位贡献的人。
我看过几个在 ListView 上实现的 SwipeView 示例 (here),但这些是针对列表中的个别项目而不是针对 ListView 本身。
在我的应用程序中,一个意图从一个 Listview 项目开始,然后将您发送到有问题的 Listview。从那里,我想实现某种功能,允许我从一个 ListView 滑动到另一个。在这种情况下,一直用意图向下钻取不会像 SwipeView 那样提供 user-friendly 体验。
如果我没有解释清楚,请告诉我。如果我必须创建额外的 classes 和 XML 布局文件来执行此操作,那就这样吧。
CustomListAdapter.java(保存ListView代码的class)
public class CustomListAdapter extends ArrayAdapter<String> {
private Context mContext;
private int id;
private ArrayList<String> callData;
private String[] headers = {"Number", "Customer Number", "Name", "Bill to Customer Number", "Bill to Name",
"Order Date", "Order Time", "Response Date", "Response Time", "Fix by Date", "Fix by Time",
"Responded Date", "Responded Time", "Fixed Date", "Fixed Time", "Your Reference",
"Description", "Transaction Status", "Ship to Code", "Ship to Name", "Ship to Address",
"Ship to Address 2", "Ship to City", "Ship to County", "Ship to Postcode", "Contact Name",
"Phone Number", "Note", "Sources"};
public CustomListAdapter(Context context, int textViewResourceId, ArrayList<String> callData) {
super(context, textViewResourceId, callData);
this.callData = callData;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.list_service_order, null);
}
String test = callData.get(position);
if (test != null) {
TextView customerNoLabel = (TextView) view.findViewById(R.id.name_label);
TextView customerNoData = (TextView) view.findViewById(R.id.listed_data);
if (customerNoLabel != null) {
customerNoLabel.setText(headers[position]);
customerNoData.setText(callData.get(position));
}
}
return view;
}
}
CallActivity2.java(实现 CustomListAdapter 代码的 class,我想用它滑动到具有不同标题和值的两个不同列表视图)
public class CallActivity2 extends ListActivity {
private ArrayList<String> individualCallData;
// private ArrayAdapter<String> callDataAdapter;
private CustomListAdapter callDataAdapter;
private ListView callDataView;
private Runnable viewParts;
private TextView serviceOrderNo;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.call_display);
Intent intent = getIntent();
individualCallData = intent.getStringArrayListExtra("Call");
// Instantiate CustomListAdapter class
callDataView = (ListView) findViewById(android.R.id.list);
callDataAdapter = new CustomListAdapter(this, R.layout.list_service_order, individualCallData);
setListAdapter(callDataAdapter);
viewParts = new Runnable(){
public void run(){
handler.sendEmptyMessage(0);
}
};
// here we call the thread we just defined - it is sent to the handler below.
Thread thread = new Thread(null, viewParts, "MagentoBackground");
thread.start();
}
private Handler handler = new Handler() {
public void handleMessage(Message msg)
{
// create some objects
// here is where you could also request data from a server
// and then create objects from that data.
//individualCallData.add("Test");
callDataAdapter = new CustomListAdapter(CallActivity2.this, R.layout.list_service_order, individualCallData);
// display the list.
setListAdapter(callDataAdapter);
}
};
}
我推荐使用像这样的库https://github.com/xenione/SwipeLayout 并且不要尝试自己做。并且请避免使用 ListView,而是使用 RecyclerView。
我认为您正在寻找 ViewPager
。在这种情况下,您不需要创建更多 xml 个文件(除非您希望每个列表中的列表项看起来不同),但您将需要创建一些 Fragments
。这样,你可以为每个列表创建一个新的Fragment
,ViewPager
会处理滑动手势在两者之间来回切换。
相反,我决定摆脱 ListView 并改用 ScrollView。我可以做的事情更加灵活。此外,我也可以滚动和滑动。 Listview 不会那样做,也不是项目点击。不过还是感谢两位贡献的人。