【问题标题】:Android - Using SwipeView for an entire ListViewAndroid - 对整个 ListView 使用 SwipeView
【发布时间】:2016-08-15 20:22:31
【问题描述】:

我见过几个在 ListView 上实现的 SwipeView 示例 (here),但这些示例适用于列表中的单个项目,而不适用于 ListView 本身。

在我的应用程序中,意图从一个 Listview 项目开始,并将您发送到相关的 Listview。从那里,我想实现某种功能,允许我从一个 ListView 滑动到另一个。在这种情况下,始终根据意图向下钻取不会像 SwipeView 那样提供用户友好的体验。

如果我没有解释清楚,请告诉我。如果我必须创建额外的类和 XML 布局文件来执行此操作,那就这样吧。

CustomListAdapter.java(保存 ListView 代码的类)

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 代码的类,我想用它滑动到具有不同标题和值的两个不同列表视图)

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

        }
    };
}

【问题讨论】:

  • 这两个列表是否在主列表和详细列表之间滑动(即第一个列表中的列表项是否与其自己的列表相对应)?还是这两个列表不相关?

标签: java android listview swipeview


【解决方案1】:

我建议使用像这样的库https://github.com/xenione/SwipeLayout 不要试图自己做。并且请避免使用 ListView 改用 RecyclerView。

【讨论】:

  • RecyclerView 支持使用ItemTouchHelper 原生刷卡,但除非我弄错了,这不是@Chosen1 所要求的。
【解决方案2】:

我认为您正在寻找ViewPager。在这种情况下,您不需要创建更多的 xml 文件(除非您希望每个列表中的列表项看起来不同),但您需要创建一些 Fragments。这样,您可以为每个列表创建一个新的FragmentViewPager 将处理滑动手势在两者之间来回移动。

【讨论】:

    【解决方案3】:

    相反,我决定摆脱 ListView 并改用 ScrollView。我可以做的事情有更大的灵活性。此外,我也可以滚动和滑动。 Listview 不这样做,它不是一个项目点击。谢谢你们两个做出贡献的人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-11
      • 1970-01-01
      • 2012-08-14
      相关资源
      最近更新 更多