Changing the visibility of a textview in a listvie

2019-05-29 04:15发布

问题:

I have a listview which is build up of two textviews coming from a separate layout file. I use a BaseAdapter to build the list from a JSON file.

I would like the first textview (Caption) to be clickable, if clicked that it shows the second textview (Text), and if clicked again that it hides it.

When I use an onClick (android:onClick="ClText") I get an error. I think I should use something of an onClickListener, but since I'm new to Android I'm not quite sure how to use it.

Can someone help me out with the code please?

回答1:

You just need to set the onClickListener for the first item in the getView method of your adapter class that extends BaseAdapter. Here's an example to illustrate what you're trying to do.

public class CustomAdapter extends BaseAdapter{
    private ArrayList<Thing> mThingArray;

    public CustomAdapter(ArrayList<Thing> thingArray) {
        mThingArray = thingArray;
    }

    // Get the data item associated with the specified position in the data set.
    @Override
    public Object getItem(int position) {
        return thingArray.get(position);
    }

    // Get a View that displays the data at the specified position in the data set.
    // You can either create a View manually or inflate it from an XML layout file.
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if(convertView == null){
            // LayoutInflater class is used to instantiate layout XML file into its corresponding View objects.
            LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.one_of_list, null);
        }

        TextView captionTextView = (TextView) convertView.findViewById(R.id.caption);
        TextView txt2 = (TextView)findViewById(R.id.text);

        captionTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           if(txt2.getVisibility() == View.INVISIBLE){
           txt2.setVisibility(View.VISIBLE);
        } else {
           txt2.setVisibility(View.INVISIBLE);
        }
       }
    });

        return convertView;
    }
}


回答2:

Here is an example on how to use a click listener with java:

TextView txt = (TextView )findViewById(R.id.TextView1);
TextView txt2 = (TextView )findViewById(R.id.TextView2);
txt.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        txt.setVisibility(View.GONE);
        txt2.setVisibility(View.VISIBLE);
    }
});

Honestly though, instead of changing the visibility of different textViews, why don't you just alter the TextView text? It would be much simpler and wouldn't require multiple TextViews.



回答3:

If you just want to switch between two textview , you can simply use a ViewSwitcher. it allow you to switch between the view that are into it. You just have to call the nextView() method and this method is cycling, so you can call nextView() endlessly

You will be able to change the view that is display.

Then you can add the same onClickListener to these to textview write something like this :

TextView txt = (TextView )findViewById(R.id.TextView1);

txt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
      viewSwitcher.nextView();
}
});