在网格视图中单击项目的错误位置

时间:2016-10-25 11:36:58

标签: android android-layout gridview adapter

我正在使用网格视图,在每个网格单元格中,我正在显示相机预览,indexOfString yourString = "lorem(ipsum)"; String [] charsToReplace = new Array ('(', ')'); for (String thisChar : charsToReplace) { while (yourString.indexOf(thisChar) > -1) { // do something with ImageSpan or something } } 。我希望当我点击TextView时,它的可见性应该消失,只有LinearLayout可见。它工作正常,但有时我遇到问题。假设现在只有两个单元格可见,当我点击它应该消失的单元格LinearLayout但是单元格2的TextView消失了。

LinearLayout

3 个答案:

答案 0 :(得分:0)

请勿在{{1​​}}使用mlayoutposition变量中使用clicklistener变量,并在getView方法参数中设置position变量 final

position

OR

onClickListener方法中的一个简单的行:

public View getView(final int position, View convertView, ViewGroup parent) { Log.d(LOG_TAG, "vikash: gridView Position position: " + position); final CameraItem camera = getItem(position); if (convertView == null) { LayoutInflater inflater = LayoutInflater.from(getContext()); convertView = (LinearLayout) inflater.inflate(R.layout.camera_cell, null); } final int mlayoutPosition; mlayoutPosition = position; final LinearLayout motionLayout = (LinearLayout) convertView.findViewById(R.id.motion_details); final TextView name = (TextView) convertView.findViewById(R.id.name); motionLayout.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { camera.ignoreMotionFault(); motionLayout.setVisibility(TextView.GONE); Log.d(LOG_TAG, "vikash: motionlayout visibility gone for position: " + position); name.setVisibility(TextView.VISIBLE); } }); return convertView; }

mlayoutPosition = position;

答案 1 :(得分:0)

尝试将可见性设置为LinearLayout.GONE而不是textView.Gone

答案 2 :(得分:0)

尝试使用以下内容: -

public View getView(int position, View convertView, ViewGroup parent) {
        Log.d(LOG_TAG,"vikash: gridView Position position: "+position);
       CameraItem camera = getItem(position);
        ViewHolder viewHolder=null;
        if(convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = (LinearLayout)inflater.inflate(R.layout.camera_cell, null);
             viewHolder=new ViewHolder();
             viewHolder.motionLayout= (LinearLayout)convertView.findViewById(R.id.motion_details);
             viewHolder.name = (TextView)convertView.findViewById(R.id.name);
             convertView.setTag(viewHolder);
        }else{
             viewHolder=(ViewHolder)convertView.getTag();
         }
        // if you want to get position on click of that view set tag position to that view
    viewHolder.motionLayout.setTag(viewHolder);
            viewHolder.motionLayout.setOnClickListener(new OnClickListener(){
                @Override
                public void onClick(View v) {
                    camera.ignoreMotionFault();
                    ViewHolder view_holder=(ViewHolder)v.getTag();
                    view_holder.motionLayout.setVisibility(LinearLayout.GONE);
                    view_holder.name.setVisibility(TextView.VISIBLE);
                }
            });
          return convertView;
}

public class ViewHolder{
LinearLayout motionLayout;
TextView name;
}

使用ViewHolder并按标记和使用标记使用以前的视图来获取视图的位置