我想在我点击按钮时显示linearlayout可见/消失

时间:2014-07-31 05:32:22

标签: android

我想显示基于用户点击按钮的布局,当用户点击按钮时我保持在位置我只需要在特定位置可见,但我的问题是当开始可见或去基于视图的用户时点击按钮,布局对于所有位置都是可见的,如何显示linerlauout仅对用户克隆位置是可见的。

private String[] mOptinItems = new String[] { "achsdsgdddddddddddddd",
        "achsdsgdddddddddddddd" };

private String[] dateArray = new String[] { "22nd jun 1012",
        "22nd jun 1012" };
int[] myImageList = new int[] { R.drawable.img, R.drawable.img };

private boolean btnMoreStatus = false;;

private ListView mEvents_Lsit;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.eventlist);

    initViews();
    CustomMovieListViewAdapter adapter = new CustomMovieListViewAdapter(
            getApplicationContext(), R.layout.event_item);

    mEvents_Lsit.setAdapter(adapter);
    // addItemsToLL();

}

private void initViews() {

    mEvents_Lsit = (ListView) findViewById(R.id.settings_list_ll);

}

/**
 * This Adapter is used forto the set items to movie item list.
 * 
 * @author imaddikunta
 * 
 */
public class CustomMovieListViewAdapter extends BaseAdapter {

    Context context;

    ViewHolder holder;

    public CustomMovieListViewAdapter(Context context, int resourceId) {
        super();
        this.context = context;

    }

    /* private view holder class */
    private class ViewHolder {
        ImageView eventImageView;
        TextView tTitle;
        TextView date;
        TextView eventTitle;
        TextView eventText;
        Button showText;
        LinearLayout llShowText;
    }

    public View getView(final int position, View convertView,
            ViewGroup parent) {

        holder = null;

        // final MovieItemBean movie = mMovieItemsList.get(position);
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.event_item, null);
            holder = new ViewHolder();
            holder.tTitle = (TextView) convertView
                    .findViewById(R.id.tvTittle);
            holder.date = (TextView) convertView.findViewById(R.id.tvDate);
            holder.eventImageView = (ImageView) convertView
                    .findViewById(R.id.event_img_view);

            holder.llShowText = (LinearLayout) convertView
                    .findViewById(R.id.lleventDisplay);
            holder.showText = (Button) convertView
                    .findViewById(R.id.showText);

            convertView.setTag(holder);
        } else

            holder = (ViewHolder) convertView.getTag();

        holder.tTitle.setText(mOptinItems[position]);
        holder.date.setText(dateArray[position]);
        holder.eventImageView.setImageResource(myImageList[position]);

        if (btnMoreStatus) {
            holder.llShowText.setVisibility(View.VISIBLE);
        } else {
            holder.llShowText.setVisibility(View.GONE);
        }

        holder.showText.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                if (holder.llShowText.getVisibility() == View.GONE) {

                    holder.llShowText.setVisibility(View.VISIBLE);
                    btnMoreStatus = true;
                    notifyDataSetChanged();

                } else {

                    btnMoreStatus = false;
                    holder.llShowText.setVisibility(View.GONE);
                    notifyDataSetChanged();
                }
            }
        });

        return convertView;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mOptinItems.length;
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}
enter code here

2 个答案:

答案 0 :(得分:0)

请尝试这种方式,希望这有助于您解决问题。

    private ArrayList<HashMap<String,Object>> listData;
    private ListView mEvents_Lsit;
    private CustomMovieListViewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mEvents_Lsit = (ListView) findViewById(R.id.settings_list_ll);
        prePareListData();
        adapter = new CustomMovieListViewAdapter(this,listData);
        mEvents_Lsit.setAdapter(adapter);
    }

    private void prePareListData(){
        listData = new ArrayList<HashMap<String, Object>>();
        HashMap<String,Object> row1 = new HashMap<String, Object>();
        row1.put("name","Android1");
        row1.put("date","22nd jun 1012");
        row1.put("image",R.drawable.ic_launcher);
        row1.put("isSelected",false);
        listData.add(row1);
        HashMap<String,Object> row2 = new HashMap<String, Object>();
        row2.put("name","Android2");
        row2.put("date","23nd jun 1012");
        row2.put("image",R.drawable.ic_launcher);
        row2.put("isSelected",false);
        listData.add(row2);
        HashMap<String,Object> row3 = new HashMap<String, Object>();
        row3.put("name","Android3");
        row3.put("date","24nd jun 1012");
        row3.put("image",R.drawable.ic_launcher);
        row3.put("isSelected",false);
        listData.add(row3);
    }

    public class CustomMovieListViewAdapter extends BaseAdapter {

        private Context context;
        private ArrayList<HashMap<String,Object>> listData;

        public CustomMovieListViewAdapter(Context context,ArrayList<HashMap<String,Object>> listData) {
            this.listData=listData;
            this.context = context;

        }

        private class ViewHolder {
            ImageView eventImageView;
            TextView tTitle;
            TextView date;
            TextView eventTitle;
            TextView eventText;
            Button showText;
            LinearLayout llShowText;
        }

        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.event_item, null);
                holder.tTitle = (TextView) convertView.findViewById(R.id.tvTittle);
                holder.date = (TextView) convertView.findViewById(R.id.tvDate);
                holder.eventImageView = (ImageView) convertView.findViewById(R.id.event_img_view);
                holder.llShowText = (LinearLayout) convertView.findViewById(R.id.lleventDisplay);
                holder.showText = (Button) convertView.findViewById(R.id.showText);

                convertView.setTag(holder);
            } else{
                holder = (ViewHolder) convertView.getTag();
            }


            holder.tTitle.setText((String)listData.get(position).get("name"));
            holder.date.setText((String)listData.get(position).get("date"));
            holder.eventImageView.setImageResource((Integer)listData.get(position).get("image"));

            if ((Boolean)listData.get(position).get("isSelected")) {
                holder.llShowText.setVisibility(View.VISIBLE);
            } else {
                holder.llShowText.setVisibility(View.GONE);
            }

            holder.showText.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if ((Boolean)listData.get(position).get("isSelected")) {
                        listData.get(position).put("isSelected", false);
                        notifyDataSetChanged();
                    } else {
                        listData.get(position).put("isSelected", true);
                        notifyDataSetChanged();
                    }
                }
            });

            return convertView;
        }

        @Override
        public int getCount() {
            return listData.size();
        }

        @Override
        public Object getItem(int pos) {
            return listData.get(pos);
        }

        @Override
        public long getItemId(int pos) {
            return pos;
        }
    }

答案 1 :(得分:0)

在ListViewAdapter中,您需要删除构建列表的项目列表View

所以你需要从mOptinItems,dateArray,myImageList中删除项目

然后设置notifyDataSetChanged

final ArrayAdapter adapter = ((ArrayAdapter)getListAdapter());
runOnUiThread(new Runnable() {
public void run() {
adapter.notifyDataSetChanged();
}
});