setOnItemClickListener无法使用自定义TextView ListView适配器

时间:2015-12-18 06:42:12

标签: java android listview android-listview android-arrayadapter

我有TextView ListView,其中包含fonts选项。当用户按下其中一种字体时,会调用一个函数来更改其他Typeface的{​​{1}}。

TextView

为什么这不起作用?它似乎根本无法检测到点击。

这是我的自定义适配器:

final ListView fontsListView = (ListView) this.view.findViewById(R.id.MenuLayout);
final String[] fonts = new String[] {
        "Aclonica",
        "Arimo-Regular",
        "Calligraffitti-Regular",
        "CherryCreamSoda",
        "Chewy",
        "ComingSoon",
        "Cousine-Regular",
        "CraftyGirls",
        "CreepsterCaps-Regular",
        "Crushed",
        "FontdinerSwanky",
        "HomemadeApple",
        "IrishGrover",
        "Nokora-Regular",
        "JustAnotherHand",
        "Kranky",
        "LuckiestGuy",
        "MaidenOrange",
        "Montez-Regular",
        "MountainsofChristmas-Regular"
};
ArrayList<String> fontsList = new ArrayList<String>();
fontsList.addAll( Arrays.asList(fonts) );
ArrayAdapter<String> listAdapter = new AdapterFontViewHolder(getActivity(), fonts);
fontsListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        Aclonica();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
});
fontsListView.setAdapter(listAdapter);

我应该在自定义适配器中设置ClickListener吗?如果是这样,我将如何做到这一点?

更新答案:

public class AdapterFontViewHolder extends ArrayAdapter<String> {
    private final Context context;
    private final String[] values;
    private Typeface[] fonts;

    public AdapterFontViewHolder(Context context, String[] values) {
        super(context, R.layout.fonts_simple_row, values);
        this.context = context;
        this.values = values;
        // Create Typeface only once and use it.. Path you can change as per your directory
        fonts = new Typeface[]{
                Typeface.createFromAsset(context.getAssets(), "fonts/Aclonica.ttf"),
                Typeface.createFromAsset(context.getAssets(), "fonts/Arimo-Regular.ttf"),
                Typeface.createFromAsset(context.getAssets(), "fonts/Calligraffitti-Regular.ttf"),
                Typeface.createFromAsset(context.getAssets(), "fonts/CherryCreamSoda.ttf"),
                Typeface.createFromAsset(context.getAssets(), "fonts/Chewy.ttf"),
                Typeface.createFromAsset(context.getAssets(), "fonts/ComingSoon.ttf"),
                Typeface.createFromAsset(context.getAssets(), "fonts/Cousine-Regular.ttf"),
                Typeface.createFromAsset(context.getAssets(), "fonts/CraftyGirls.ttf"),
                Typeface.createFromAsset(context.getAssets(), "fonts/CreepsterCaps-Regular.ttf"),
                Typeface.createFromAsset(context.getAssets(), "fonts/Crushed.ttf"),
                Typeface.createFromAsset(context.getAssets(), "fonts/FontdinerSwanky.ttf"),
                Typeface.createFromAsset(context.getAssets(), "fonts/HomemadeApple.ttf"),
                Typeface.createFromAsset(context.getAssets(), "fonts/IrishGrover.ttf"),
                Typeface.createFromAsset(context.getAssets(), "fonts/Nokora-Regular.ttf"),
                Typeface.createFromAsset(context.getAssets(), "fonts/JustAnotherHand.ttf"),
                Typeface.createFromAsset(context.getAssets(), "fonts/Kranky.ttf"),
                Typeface.createFromAsset(context.getAssets(), "fonts/LuckiestGuy.ttf"),
                Typeface.createFromAsset(context.getAssets(), "fonts/MaidenOrange.ttf"),
                Typeface.createFromAsset(context.getAssets(), "fonts/Montez-Regular.ttf"),
                Typeface.createFromAsset(context.getAssets(), "fonts/MountainsofChristmas-Regular.ttf"),
        };
    }

    public class ViewHolder {
        TextView textView;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vHolder;
        if (convertView == null) {
            // Create Instnce of view if its null & store object in ViewHolder (a class)
            vHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.fonts_simple_row, parent, false);
            vHolder.textView = (TextView) convertView.findViewById(R.id.rowTextView);

            //Set ViewHolder instance in convertview in a tag as a object
            convertView.setTag(vHolder);
        } else {
            // reuse as already converview is instansiated & it holds ViewHolder instance in tag
            vHolder = (ViewHolder) convertView.getTag();
        }

        vHolder.textView.setText(values[position]);

        //Here From Fonts array take Typeface
        vHolder.textView.setTypeface(fonts[position]);

//        vHolder.textView.setOnClickListener((View.OnClickListener) fonts[position]);

        return convertView;
    }
}

这是被调用的函数,根据ListView中的位置,设置不同的字体:

fontsListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> adapter, View v, int position,
                                    long arg3)
            {
                LaunchFont(fonts[position]);
            }
        });

这样我就不必为每个字体都有一个独特的功能。

3 个答案:

答案 0 :(得分:1)

setOnItemClickListener而不是setOnItemSelectedListener

fontsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Aclonica();
         }
     });

答案 1 :(得分:1)

改变这个:

fontsListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()

fontsListView.setOnItemClickListener(new AdapterView.OnItemClickListener()

答案 2 :(得分:1)

在以下行之后:

final ListView fontsListView = (ListView) this.view.findViewById(R.id.MenuLayout);

添加以下代码:

fontsListView.setOnItemClickListener(new OnItemClickListener()
   {
      @Override
      public void onItemClick(AdapterView<?> adapter, View v, int position,
            long arg3) 
      {
            String value = fonts[position] ;
            // assuming string and if you want to get the value on click of list item
            // do what you intend to do on click of listview row
      }
   });