【问题标题】:How to make custom ListView?如何制作自定义 ListView?
【发布时间】:2019-02-20 20:56:02
【问题描述】:

我想从 listView 制作一个可重复使用的 Listview 控件,其中的列可以被控制,假设我想加载 3 列列表视图,有时是 2 有时是 4。 如何以编程方式控制列表视图的列和行。根据我的 json 值,我将显示列表。 另外我想让一些列也可以编辑。这也需要由代码级别控制

这是我开始的代码:

import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;

import org.json.JSONArray;
import org.json.JSONObject;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class LayoutAdvancedList extends ListView {
    private String m_name;
    private int m_editMask = 0;
    private int m_EditedRowIndex = 0;
    private int m_EditedFieldIndex = 0;
    public String getName() {
        return m_name;
    }
    public void setName(String name) {
        this.m_name = name;
    }
    public void setMaxLength(final int maxLength) {
        if (maxLength > 0) {
           // super.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)});
        } else {
         //   super.setFilters(new InputFilter[]{});
        }
    }
    public void setReadOnly(final boolean readOnly) {
        super.setFocusable(!readOnly);
        super.setFocusableInTouchMode(!readOnly);
        super.setClickable(!readOnly);
        super.setLongClickable(!readOnly);
       // super.setCursorVisible(!readOnly);
    }
    public LayoutAdvancedList(Context context) {
        super(context);
        LayoutInitialize(context);
    }
    public LayoutAdvancedList(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInitialize(context);
    }
    public LayoutAdvancedList(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        LayoutInitialize(context);
    }
    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        if (enabled) {
            this.getBackground().setColorFilter(null);
        } else {
            this.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
        }
    }

【问题讨论】:

  • 你能贴出你想要实现的UI快照
  • 你为什么不使用回收站视图?

标签: android listview


【解决方案1】:

如果您有选择,您应该为此使用 RecyclerView 和 GridLayoutManager,以便您可以动态选择列数:

recyclerView.setLayoutManager(new GridLayoutManager(this, numberOfColumns));

Here's an example of how to make a RecyclerView

【讨论】:

    【解决方案2】:

    大概您的ListView 中的每个项目都由子视图定义,这些子视图排列成每行中的 2-4 列,具体取决于您的要求。只需使用view.setVisibility(View.GONE)view.setVisibility(View.VISIBLE)view.setVisibility(View.INVISIBLE) 以编程方式控制每列的​​存在/不存在

    【讨论】:

    • 如果我不知道服务器可以预期的列数怎么办,如果服务器说需要显示 n 个列,我该如何控制?所以主要我的问题是如何根据来自服务器的 json 创建列
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多