Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ListView项目在向上滚动时重叠_Android_Xml_Eclipse - Fatal编程技术网

Android ListView项目在向上滚动时重叠

Android ListView项目在向上滚动时重叠,android,xml,eclipse,Android,Xml,Eclipse,大家好,我正在使用一个带有LayoutFlater的listView,当我向下滚动列表视图时,它是ok的,但当我向上滚动列表视图时,它开始重叠 事实上,一旦第一行退出屏幕,问题就出现了。 下面是Java代码 public class VehicleActivity extends Activity { static ListView listView; public int context_menu_index; public void onCreate(Bundle savedInst

大家好,我正在使用一个带有LayoutFlater的listView,当我向下滚动列表视图时,它是ok的,但当我向上滚动列表视图时,它开始重叠

事实上,一旦第一行退出屏幕,问题就出现了。

下面是Java代码

public class VehicleActivity extends Activity
{ 
 static ListView listView;
 public int context_menu_index;
 public void onCreate(Bundle savedInstanceState) 
 {
   //Some Code
   listView = (ListView) findViewById(R.id.list_view);
  listView.setAdapter(new EfficientAdapter(this));
 }

private class EfficientAdapter extends BaseAdapter
{
   private LayoutInflater mInflater;
   private TextView text1, text2, text3; 
   private View listItem;
   private ImageView  img_option;

   //Some Code
   public View getView(final int position, View convertView, ViewGroup parent) 
   {   
      if (convertView == null) 
      {
        mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         listItem = mInflater.inflate(R.layout.three_col_row, null);


        text1 = (TextView)  listItem.findViewById(R.id.imei);
        text2 = (TextView)  listItem.findViewById(R.id.status);
        text3 = (TextView)  listItem.findViewById(R.id.location);
        img_option = (ImageView) listItem.findViewById(R.id.img_arrow);

        img_option.setOnClickListener(new OnClickListener() 
        {
           @Override
           public void onClick(View v) 
           {
             VehicleActivity.this.registerForContextMenu(listView);                                           VehicleActivity.this.openContextMenu(listView);

              context_menu_index = position;
           }
       })
       img_option.setFocusable(true);
       img_option.setClickable(true);
       img_option.setTag(getItem(position));

       //Setting The Font Style
       text1.setTypeface(null, Typeface.BOLD);
       text2.setTypeface(null,  Typeface.ITALIC);  

       //Passing The Actual Values To TextViews
       text1.setText("ID: "       +VehicleList.IMEI[position]);
       text2.setText("Status: "   +VehicleList.Status[position]);
       text3.setText("Location: " +VehicleList.Location[position]);

       listItem.setBackgroundColor(position % 2 == 0 ? Color.parseColor("#eaeaea") : Color.parseColor("#d3e3f3"));
      } 

     return listItem;
 }
} }

下面是包含ListView的活动布局

<LinearLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:showAsAction="always"
  android:background="#0066FF" >

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusableInTouchMode="false"  
        android:cacheColorHint="#00000000"
        android:dividerHeight="1dp"
        android:scrollbars="none">
    </ListView>

 </LinearLayout>

这是我要传递给listView的布局

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/layout_main"
  android:layout_height="wrap_content"
  android:layout_width="fill_parent"
  android:orientation="horizontal"
  android:padding="4dp">

   <LinearLayout
     android:id="@+id/child_lay1"    
     android:layout_height="wrap_content"
     android:layout_width="320dp"
     android:orientation="vertical"
     android:gravity="start">

            <TextView
            android:id="@+id/imei"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:layout_marginLeft="10dp"
            android:textSize="15sp"
            android:textStyle="bold"
            android:textColor="#000000"/>

            <TextView
            android:id="@+id/status"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:layout_marginLeft="10dp"
            android:textSize="15sp"
            android:textStyle="bold"
            android:textColor="#000000"/>

            <TextView
            android:id="@+id/location"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:layout_marginLeft="10dp"
            android:textSize="15sp"
            android:textColor="#000000"/>

    </LinearLayout>  <!-- Child Vertical Linear Layout -->

    <LinearLayout
    android:id="@+id/child_lay2"    
    android:layout_height="30dp"
    android:layout_width="30dp"
    android:orientation="vertical"
    android:gravity="end">

        <ImageView 
         android:id="@+id/img_arrow"
         android:src="@drawable/arrow"
         android:layout_width="30dp"
         android:layout_height="30dp"/>

   </LinearLayout> 

</LinearLayout>  

我认为您的适配器不正确,请检查建议的适配器。尝试使用ViewHolder,以便可以重用视图

public View getView(final int position, View convertView, ViewGroup parent) 
   { 
 ViewHolder vh;
if (convertView == null) {
            //inflate your view here.
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }

        return convertView;
}

尝试在EfficientAdapter中添加ViewHolder类,并按如下方式更改代码:-

public class ViewHolder 
    {
         TextView text1;
         TextView text2;
         TextView text3;
         ImageView img_option;
    }


请上传您的java源代码……请检查代码@RajatsharmaLet me try,但上次我使用ViewHolder时,我在img_选项上遇到了NullPointerException。setOnClickListener(新OnClickListener(){})@用户3820076非常感谢,没有任何问题。现在的问题是出了什么问题?再次感谢你,伙计,在过去的15个小时里,我一直在努力解决这个问题。
    public View getView(final int position, View convertView, ViewGroup parent) 
   {   ViewHolder holder;
      if (convertView == null) 
      {
        mInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.three_col_row, null);

         holder = new ViewHolder();

         holder.text1 = (TextView)  convertView.findViewById(R.id.imei);
         holder.text2 = (TextView)  convertView.findViewById(R.id.status);
         holder.text3 = (TextView)  convertView.findViewById(R.id.location);
         holder.img_option = (ImageView) convertView.findViewById(R.id.img_arrow);
         convertView.setTag(holder);
        holder.img_option.setOnClickListener(new OnClickListener() 
        {
           @Override
           public void onClick(View v) 
           {
             VehicleActivity.this.registerForContextMenu(listView);                                           
             VehicleActivity.this.openContextMenu(listView);

             int context_menu_index = position;
           }
       });

      }  else {
            holder = (ViewHolder) convertView.getTag();
        }
      holder.img_option.setFocusable(true);
      holder.img_option.setClickable(true);
     holder.img_option.setTag(holder);

       //Setting The Font Style
      holder.text1.setTypeface(null, Typeface.BOLD);
      holder.text2.setTypeface(null,  Typeface.ITALIC);  

       //Passing The Actual Values To TextViews
      holder. text1.setText("ID: "       +VehicleList.IMEI[position]);
      holder.text2.setText("Status: "   +VehicleList.Status[position]);
      holder.text3.setText("Location: " +VehicleList.Location[position]);

      convertView.setBackgroundColor(position % 2 == 0 ? Color.parseColor("#eaeaea") : Color.parseColor("#d3e3f3"));

     return convertView;
 }