本文介绍了列表视图嵌入到Android的preferences的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要显示在preferences列表视图,所以我创建了自己的类(继承 preference ),然后将其设置是这样的:

I need to show a list in Preferences view, so I created my own class (inherit of Preference) and then set it like this:

@Override
protected View onCreateView(ViewGroup parent){

    cards = new ArrayList<String>();

    // Test
    cards.add("4859-2368957415");
    cards.add("4859-5987412598");

    LinearLayout layout = new LinearLayout(getContext()); 
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT); 
    layout.setOrientation(LinearLayout.VERTICAL); 

    list = new ListView(getContext()); 
    list.setLayoutParams(params); 
    layout.addView(list); 
    adapter = new ArrayAdapter<String>(getContext(), 
            android.R.layout.simple_list_item_1, cards); 
    list.setAdapter(adapter); 
    return layout; 
}

这工作,但我不能滚动(它显示真正​​小)。我认为我的列表视图被嵌入到preference(有自己的宽度,并以默认的高度),但我需要表现出完全名单。

This works, but I can't scroll it (it shows really tiny). I think that my Listview is embed into a Preference (with its own width and height by default), but I need to show a completely list.

我怎样才能实现呢?谢谢!

How can I achieve it? Thanks!

推荐答案

我解决我自己的问题加入 preferenceScreen 动态,而不是嵌入的ListView 组件到 preference

I solved my own problem adding PreferenceScreen dynamically instead of embedding a ListView component into a Preference.

findPreference("addmyaccount").setOnPreferenceClickListener(new OnPreferenceClickListener() {
    @Override
    public boolean onPreferenceClick(Preference preference) {

        SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();

        // Get preference category and add a custom preference (simulating a ListView)
        PreferenceCategory targetCategory = (PreferenceCategory)findPreference("myaccounts");
        final Preference account = new Preference(Settings.this);

        account.setKey("mykey");

        account.setTitle("Custom Preference");

        targetCategory.addPreference(account);
        editor.putString("mykey", "Custom Preference");
        editor.commit();

    }
});

这篇关于列表视图嵌入到Android的preferences的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 12:45