preference列表只显示第一个元素

preference列表只显示第一个元素

本文介绍了preference列表只显示第一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个 preferenceActivity 自定义 preference 的意见。我的问题是,我创建了一个带有的ListView 的视图,它只显示第一个元素。我后我的code和图像:
http://imageshack.us/photo/my-images/545/sc20120307161530.png/

XML:

 <?XML版本=1.0编码=UTF-8&GT?;
< preferenceScreen的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>    < preferenceCategory
        机器人:键=player_settings
        机器人:标题=@字符串/ settings_player_config>
        < EditText上preference
            机器人:设置defaultValue =@字符串/ settings_player_default_name
            机器人:dialogMessage =@字符串/ settings_player_summary
            机器人:DIALOGTITLE =@字符串/ settings_playersname
            机器人:键=PLAYER_NAME
            机器人:总结=@字符串/ settings_player_summary
            机器人:标题=@字符串/ settings_playersname/>
    < / preferenceCategory>
    < preferenceCategory
        机器人:键=卷
        机器人:标题=@字符串/ settings_volume>
        < com.battleship preferences.SeekBar preferences
            机器人:设置defaultValue =50
            机器人:键=卷
            机器人:标题=@字符串/ settings_volume/>
    < / preferenceCategory>
    < preferenceCategory
        机器人:键=闪耀
        机器人:标题=@字符串/ settings_shine>
        < com.battleship preferences.SeekBar preferences
            机器人:设置defaultValue =50
            机器人:键=闪耀
            机器人:标题=@字符串/ settings_shine/>
    < / preferenceCategory>
    < preferenceCategory
        机器人:键=themeTitle
        机器人:标题=@字符串/ settings_group_themes>
        < com.battleship preferences.List preferences安卓键=主旋律/>
    < / preferenceCategory>
    < preferenceCategory
        机器人:键=fontsTitle
        机器人:标题=@字符串/ settings_group_font_size>
        < com.battleship preferences.List preferences安卓键=FONT/>
    < / preferenceCategory>< / preferenceScreen>

自定义列表preference:

 包com.battleship preferences。进口com.battleship.R;进口android.content.Context;
进口android.content.Shared preferences;
进口android.media.AudioManager;
。进口的Andr​​oid preference preference;
进口android.util.AttributeSet;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.ArrayAdapter;
进口android.widget.LinearLayout;
进口android.widget.ListView;
进口android.widget.RadioButton;
进口android.widget.RadioGroup;
进口android.widget.RadioGroup.OnCheckedChangeListener;
进口android.widget.Toast;公共类List preferences扩展preference工具
        OnCheckedChangeListener {    公开名单preferences(上下文的背景下){
        超级(上下文);
    }    公开名单preferences(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
    }    公开名单preferences(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
        超(背景下,ATTRS,defStyle);
    }    @覆盖
    保护无效的onClick(){        super.onClick();
        吐司T = Toast.makeText(的getContext(),HOLA!,3);
        t.show();
    }    @覆盖
    受保护的视图onCreateView(ViewGroup中父){        的String [] = contentString新的String [3];
        如果(getKey()。等于(主题)){
            contentString =新的String [] {
                    (的getContext()的getString(R.string.settings_theme_default))
                    (的getContext()的getString(R.string.settings_theme_black))
                    (的getContext()的getString(R.string.settings_theme_white))};
        }其他{
            contentString =新的String [] {
                    (的getContext()的getString(R.string.settings_font_big))
                    (的getContext()的getString(R.string.settings_font_medium))
                    (的getContext()的getString(R.string.settings_font_little))};
        }        ListView控件的ListView =新的ListView(的getContext());
        ArrayAdapter<串GT;阵列=新ArrayAdapter<串GT;(的getContext()
                android.R.layout.simple_list_item_single_choice,
                android.R.id.text1,contentString);
        listView.setAdapter(数组);
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        返回的ListView;
    }    私人无效更新preference(INT intRadio){
        共享preferences.Editor编辑= getEditor();
        editor.putInt(getKey(),intRadio);
        editor.commit();
    }
}


解决方案
你不恨它

当每个人都试图告诉你,你不应该这样做的,而不是仅仅回答你的问题?能够动态生成内容是应用程序的灵活性是至关重要的,我完全知道你在做什么了。

这将工作假设所有的ListView项将大致相同的高度(非相关部分省略)。顺序是很重要的,尽量保持它,因为我已经指出:

 进口android.view.ViewGroup.OnHierarchyChangeListener;公共类List preferences扩展preference工具
    OnCheckedChangeListener,OnHierarchyChangeListener {私人的ListView ListView的;
私人查看thisView;
私人INT listHeight = 0;@覆盖
受保护的视图onCreateView(ViewGroup中父){
    this.setLayoutResource(R.layout.listview_ preference_layout);
    thisView = super.onCreateView(父);
    ListView控件=(ListView控件)thisView.findViewById(android.R.id.list);
    listView.setOnHierarchyChangeListener(本);
    的String [] = contentString新的String [3];
    如果(getKey()。等于(主题)){
        contentString =新的String [] {
        (的getContext()的getString(R.string.settings_theme_default))
        (的getContext()的getString(R.string.settings_theme_black))
        (的getContext()的getString(R.string.settings_theme_white))};
    }其他{
        contentString =新的String [] {
        (的getContext()的getString(R.string.settings_font_big))
        (的getContext()的getString(R.string.settings_font_medium))
        (的getContext()的getString(R.string.settings_font_little))};
    }    ArrayAdapter<串GT;阵列=新ArrayAdapter<串GT;(的getContext()
        android.R.layout.simple_list_item_single_choice,
        android.R.id.text1,contentString);
    listView.setAdapter(数组);
    listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    返回thisView;
}公共无效onChildViewAdded(查看父,子视图){
    INT childHeight = child.getMeasuredHeight();
    如果(childHeight大于0)
    {
        listHeight = listView.getAdapter()getCount将()* childHeight。
        thisView.setMinimumHeight(listHeight);
        Log.i(TAG,onChildViewAdded,做到:+ listHeight ++ childHeight);
    }
}公共无效onChildViewRemoved(查看父,子视图){
}
}

您还需要RES /布局/ listview_ preference_layout.xml与以下内容:

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:layout_gravity =fill_vertical | fill_horizo​​ntal |补
    机器人:重力=补
    机器人:方向=垂直
   >
    < ListView控件
        机器人:ID =@机器人:ID /列表
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =0dip
        机器人:clipChildren =真
        机器人:isScrollContainer =真
        机器人:layout_gravity =fill_vertical | fill_horizo​​ntal |补
        机器人:layout_weight =1
        机器人:choiceMode =singleChoice/>
< / LinearLayout中

I am developing a PreferenceActivity with custom Preference views. My problem is that I created a view with a ListView and it only shows the first element. I post my code and an image:http://imageshack.us/photo/my-images/545/sc20120307161530.png/

xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <PreferenceCategory
        android:key="player_settings"
        android:title="@string/settings_player_config" >
        <EditTextPreference
            android:defaultValue="@string/settings_player_default_name"
            android:dialogMessage="@string/settings_player_summary"
            android:dialogTitle="@string/settings_playersname"
            android:key="player_name"
            android:summary="@string/settings_player_summary"
            android:title="@string/settings_playersname" />
    </PreferenceCategory>
    <PreferenceCategory
        android:key="volume"
        android:title="@string/settings_volume" >
        <com.battleship.preferences.SeekBarPreferences
            android:defaultValue="50"
            android:key="volume"
            android:title="@string/settings_volume" />
    </PreferenceCategory>
    <PreferenceCategory
        android:key="shine"
        android:title="@string/settings_shine" >
        <com.battleship.preferences.SeekBarPreferences
            android:defaultValue="50"
            android:key="shine"
            android:title="@string/settings_shine" />
    </PreferenceCategory>
    <PreferenceCategory
        android:key="themeTitle"
        android:title="@string/settings_group_themes" >
        <com.battleship.preferences.ListPreferences android:key="theme" />
    </PreferenceCategory>
    <PreferenceCategory
        android:key="fontsTitle"
        android:title="@string/settings_group_font_size" >
        <com.battleship.preferences.ListPreferences android:key="font" />
    </PreferenceCategory>

</PreferenceScreen>

The Custom ListPreference:

package com.battleship.preferences;

import com.battleship.R;

import android.content.Context;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

public class ListPreferences extends Preference implements
        OnCheckedChangeListener {

    public ListPreferences(Context context) {
        super(context);
    }

    public ListPreferences(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ListPreferences(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onClick() {

        super.onClick();
        Toast t = Toast.makeText(getContext(), "HOLA!", 3);
        t.show();
    }

    @Override
    protected View onCreateView(ViewGroup parent) {



        String[] contentString = new String[3];
        if (getKey().equals("theme")) {
            contentString = new String[] {
                    (getContext().getString(R.string.settings_theme_default)),
                    (getContext().getString(R.string.settings_theme_black)),
                    (getContext().getString(R.string.settings_theme_white)) };
        } else {
            contentString = new String[] {
                    (getContext().getString(R.string.settings_font_big)),
                    (getContext().getString(R.string.settings_font_medium)),
                    (getContext().getString(R.string.settings_font_little)) };
        }

        ListView listView = new ListView(getContext());
        ArrayAdapter<String> array = new ArrayAdapter<String>(getContext(),
                android.R.layout.simple_list_item_single_choice,
                android.R.id.text1, contentString);
        listView.setAdapter(array);
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);


        return listView;
    }

    private void updatePreference(int intRadio) {
        SharedPreferences.Editor editor = getEditor();
        editor.putInt(getKey(), intRadio);
        editor.commit();
    }


}
解决方案

Don't you hate it when everyone tries to tell you that you shouldn't do it that way instead of just answering your question? Being able to dynamically generate content is crucial to app flexibility, I totally know what you're after.

This will work assuming all the ListView entries will be roughly the same height (non-pertinent parts omitted). The order is important, try to keep it as I've indicated:

import android.view.ViewGroup.OnHierarchyChangeListener;

public class ListPreferences extends Preference implements
    OnCheckedChangeListener, OnHierarchyChangeListener {

private ListView listView;
private View thisView;
private int listHeight = 0;

@Override
protected View onCreateView(ViewGroup parent) {
    this.setLayoutResource(R.layout.listview_preference_layout);
    thisView = super.onCreateView(parent);
    listView = (ListView) thisView.findViewById(android.R.id.list);
    listView.setOnHierarchyChangeListener(this);
    String[] contentString = new String[3];
    if (getKey().equals("theme")) {
        contentString = new String[] {
        (getContext().getString(R.string.settings_theme_default)),
        (getContext().getString(R.string.settings_theme_black)),
        (getContext().getString(R.string.settings_theme_white)) };
    } else {
        contentString = new String[] {
        (getContext().getString(R.string.settings_font_big)),
        (getContext().getString(R.string.settings_font_medium)),
        (getContext().getString(R.string.settings_font_little)) };
    }

    ArrayAdapter<String> array = new ArrayAdapter<String>(getContext(),
        android.R.layout.simple_list_item_single_choice,
        android.R.id.text1, contentString);
    listView.setAdapter(array);
    listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    return thisView;
}

public void onChildViewAdded(View parent, View child) {
    int childHeight = child.getMeasuredHeight();
    if(childHeight > 0)
    {
        listHeight = listView.getAdapter().getCount() * childHeight;
        thisView.setMinimumHeight(listHeight);
        Log.i(TAG,"onChildViewAdded, done: "+listHeight+" "+childHeight);
    }
}

public void onChildViewRemoved(View parent, View child) {
}


}

You'll also need res/layout/listview_preference_layout.xml with the following:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="fill_vertical|fill_horizontal|fill"
    android:gravity="fill"
    android:orientation="vertical"
   >
    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:clipChildren="true"
        android:isScrollContainer="true"
        android:layout_gravity="fill_vertical|fill_horizontal|fill"
        android:layout_weight="1"
        android:choiceMode="singleChoice"/>
</LinearLayout

这篇关于preference列表只显示第一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 02:27