具有快速滚动和字母顺序索引的Android ListView

问题描述 投票:13回答:7

如何触摸右字母面板上的字母,如图所示,如何添加testview?请你帮助我好吗?下面是我的代码。

详细来说,我正在寻找一个与下图完全相同的示例。目前,我对数据进行了排序。当我单击右侧字母面板时,它会正确显示数据。但是问题是当在右侧字母面板上触摸一个字母时,我需要显示他按image(E)所示的大号按下的字母。我该怎么办,请您帮帮我吗?预先谢谢!

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9sRnFpRS5wbmcifQ==” alt =“所需UI的屏幕截图”>

// MainActivity.java
public class MainActivity extends Activity implements
                          SearchView.OnQueryTextListener,
                          SearchView.OnCloseListener {

    private IndexableListView listView;
    private SearchView search;
    EfficientAdapter objectAdapter;
    EfficientAdapter2 objectAdapter1;
    int textlength = 0;
    private CheckBox checkStat, checkRoutine, checkTat;
    private ArrayList<Patient> patientListArray;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.homempleb);
        Log.i("scan"," txtScanResult ");

        ActionItem nextItem = new ActionItem();
        final QuickAction quickAction = new QuickAction(this, QuickAction.VERTICAL);
        quickAction.addActionItem(nextItem);
        quickAction.setOnDismissListener(new QuickAction.OnDismissListener() {
            @Override
            public void onDismiss() {
                Toast.makeText(getApplicationContext(), "Dismissed", Toast.LENGTH_SHORT).show();
            }
        });

        listView = (IndexableListView) findViewById(R.id.homelistView);
        listView.setTextFilterEnabled(true);
        listView.setFastScrollEnabled(true);
        listView.setFastScrollAlwaysVisible(true);
        objectAdapter = new EfficientAdapter(this);
        listView.setAdapter(objectAdapter);
    }

    @Override
    public boolean onClose() {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        return false;
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }
}

...

// EfficientAdapter.java
public class EfficientAdapter extends BaseAdapter implements SectionIndexer {

    private String mSections = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    ArrayList<Patient> patientListArray;
    private LayoutInflater mInflater;
    private Context context;

    public EfficientAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
        this.context=context;
        String patientListJson = CountriesList.jsonData;
        JSONObject jssson;
        try {
            jssson = new JSONObject(patientListJson);
            patientListJson = jssson.getString("PostPatientDetailResult");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Gson gson = new Gson();
        JsonParser parser = new JsonParser();
        JsonArray Jarray = parser.parse(patientListJson).getAsJsonArray();
        patientListArray = new ArrayList<Patient>();
        for (JsonElement obj : Jarray) {
            Patient patientList = gson.fromJson(obj, Patient.class);
            patientListArray.add(patientList);
            Collections.sort(patientListArray, new Comparator<Object>() {
                @Override
                public int compare(Object o1, Object o2) {
                     Patient p1 = (Patient) o1;
                     Patient p2 = (Patient) o2;
                    return p1.getName().compareToIgnoreCase(p2.getName());
                }
            });
        }
    }


    public int getCount() {
        return patientListArray.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.homemplebrowview, null);
            holder = new ViewHolder();
            holder.text1 = (TextView) convertView.findViewById(R.id.name);
            holder.text2 = (TextView) convertView.findViewById(R.id.mrn);
            holder.text3 = (TextView) convertView.findViewById(R.id.date);
            holder.text4 = (TextView) convertView.findViewById(R.id.age);
            holder.text5 = (TextView) convertView.findViewById(R.id.gender);
            holder.text6 = (TextView) convertView.findViewById(R.id.wardno);
            holder.text7 = (TextView) convertView.findViewById(R.id.roomno);
            holder.text8 = (TextView) convertView.findViewById(R.id.bedno);
            holder.btnList = (Button) convertView.findViewById(R.id.listbutton);
            holder.btnList.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // Intent next=new Intent(context, SeviceDetails.class);
                    // context.startActivity(next);
                }
            });
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.text1.setText(Util.formatN2H(patientListArray.get(position).getName()));
        holder.text2.setText(patientListArray.get(position).getMrnNumber());
        holder.text3.setText(Util.formatN2H(patientListArray.get(position).getRoom()));
        holder.text4.setText(Util.formatN2H(patientListArray.get(position).getAge()));
        holder.text5.setText(Util.formatN2H( patientListArray.get(position).getGender()));
        holder.text6.setText(Util.formatN2H(patientListArray.get(position).getWard()));
        holder.text7.setText(Util.formatN2H(patientListArray.get(position).getRoom()));
        holder.text8.setText(Util.formatN2H(patientListArray.get(position).getBed()));
        return convertView;
    }

    static class ViewHolder {
        public Button btnList;
        public TextView text8;
        public TextView text7;
        public TextView text6;
        public TextView text5;
        public TextView text4;
        public TextView text1;
        public TextView text2;
        public TextView text3;
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

    //@Override
    public int getPositionForSection(int section) {
        // If there is no item for current section, previous section will be selected
        for (int i = section; i >= 0; i--) {
            for (int j = 0; j < getCount(); j++) {
                if (i == 0) {
                    // For numeric section
                    for (int k = 0; k <= 9; k++) {
                        if (StringMatcher.match(String.valueOf(patientListArray.get(j).getName().charAt(0)), String.valueOf(k)))
                            return j;
                    }
                } else {
                    if (StringMatcher.match(String.valueOf(patientListArray.get(j).getName().charAt(0)),
                                            String.valueOf(mSections.charAt(i))))
                        return j;
                }
            }
        }
        return 0;
    }

    //@Override
        public int getSectionForPosition(int position) {
        return 0;
    }

    //@Override
    public Object[] getSections() {
        String[] sections = new String[mSections.length()];
        for (int i = 0; i < mSections.length(); i++)
            sections[i] = String.valueOf(mSections.charAt(i));
        return sections;
    }

}
android android-layout android-intent android-emulator android-widget
7个回答
4
投票

您可能想检查一下:

https://github.com/andraskindler/quickscroll

这是我创建的一个图书馆项目,因为我不得不在一些最近的应用程序中使用此滚动模式,所以我认为其他人可能对此感兴趣。它相当容易使用,请参见上面的github链接中的自述文件。


4
投票

听是您需要的https://github.com/woozzu/IndexableListView的很酷的例子

为了编译项目并摆脱韩文,请更新StringMatcher

package com.woozzu.android.util;

public class StringMatcher {
    public static boolean match(String value, String keyword) {
        if (value == null || keyword == null)
            return false;
        if (keyword.length() > value.length())
            return false;

        int i = 0, j = 0;
        do {
            int vi = value.charAt(i);
            int kj = keyword.charAt(j);
            if (isKorean(vi) && isInitialSound(kj)) {
            } else {
                if (vi == kj) {
                    i++;
                    j++;
                } else if (j > 0)
                    break;
                else
                    i++;
            }
        } while (i < value.length() && j < keyword.length());

        return (j == keyword.length())? true : false;
    }

    private static boolean isKorean(int i) {
        return false;
    }

    private static boolean isInitialSound(int i) {
        return false;
    }
}


0
投票

听是您要寻找的一个很好的例子https://github.com/woozzu/IndexableListView


0
投票

如果要显示单击的字母作为祝酒词(自定义祝酒词,如图中所示,请使用此:



0
投票

我已经在片段中显示了列表,如果您想在主要活动中显示,则可以传递它,而不是getActivity()

© www.soinside.com 2019 - 2024. All rights reserved.