how to make textview of the listview clickable

后端 未结 2 1133
渐次进展
渐次进展 2021-01-24 09:34

I have a customized listview contains of some Textviews. I set the list view to the adapter as follows:

BestandTypAdapter          


        
相关标签:
2条回答
  • 2021-01-24 09:46

    Just try to add this attribute for the TextView:

    android:focusable="false"
    
    0 讨论(0)
  • 2021-01-24 09:56

    Example for Array adapter , this one is main activity.

    public class ListViewMain extends AppCompatActivity {
    private ArrayList<BrandModel> alBrand;
    private ListView list;
    private CustomAdapter custAdapter;
    private AdapterView.OnItemClickListener messageClickedHandler;
    private View vHeader;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        setupDefaults();
        setupEvents();
    }
    
    public void init() {
        //list view
        list = (ListView) findViewById(R.id.listView);
        //Array list
        alBrand = new ArrayList<>();
        //Custom Adapter
        custAdapter = new CustomAdapter(this, R.layout.textlistview, alBrand);
        //View
        vHeader = getLayoutInflater().inflate(R.layout.listviewheader, null, false);
    }
    
    public void setupDefaults() {
        addBrandAndAdapter();
    }
    
    public void addBrandAndAdapter() {
        alBrand.add(new BrandModel(getResources().getString(R.string.And), R.drawable.android));
        alBrand.add(new BrandModel(getResources().getString(R.string.Mac), R.drawable.apple));
        alBrand.add(new BrandModel(getResources().getString(R.string.Tizen), R.drawable.insta));
        alBrand.add(new BrandModel(getResources().getString(R.string.window), R.drawable.windows));
        list.addHeaderView(vHeader);
        list.setAdapter(custAdapter);
    }
    
    public void setupEvents() {
        messageClickedHandler = new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                Toast.makeText(getApplicationContext(), "Click ListItem Number " + position, Toast.LENGTH_LONG).show();
                System.out.println("Selected");
            }
        };
        list.setOnItemClickListener(messageClickedHandler);
    }
    

    }

    1. Arrayadapter

    public class CustomAdapter extends ArrayAdapter<BrandModel> {
    ArrayList<BrandModel> alBrand = new ArrayList<>();
    
    public CustomAdapter(Context context, int tvResId, ArrayList<BrandModel> alObjects) {
        super(context, tvResId, alObjects);
        alBrand = alObjects;
    }
    
    @Override
    public int getCount() {
        return super.getCount();
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View add = convertView;
        BrandHolder holder = null;
        if (add == null) {
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            add = inflater.inflate(R.layout.textlistview, null);
            holder = new BrandHolder();
            holder.tvName = (TextView) add.findViewById(R.id.tvName);
            holder.ivImage = (ImageView) add.findViewById(R.id.ivIcon);
            add.setTag(holder);
    
        } else {
            holder = (BrandHolder) add.getTag();
        }
        holder.tvName.setText(alBrand.get(position).getStrBrdName());
        holder.ivImage.setImageResource(alBrand.get(position).getImgBrdLogo());
        return add;
    }
    
    static class BrandHolder {
        TextView tvName;
        ImageView ivImage;
    }
    

    }

    0 讨论(0)
提交回复
热议问题