Android Dev - 自定义列表视图不显示所有子组件

时间:2013-08-22 11:55:30

标签: android android-listview android-emulator baseadapter

我有一个有问题的问题我有一个自定义列表实现,它在大多数设备中都能正常工作,但仅限于少数设备,如galaxy s4或grand duos,子元素的最后一个图像不是可见。

代码如下..

主要列表视图:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/backgroundimage"
    android:keepScreenOn="true"
    android:orientation="vertical"
    tools:context=".MainActivity" >


<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:divider="#b5b5b5"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/list_selector" />

</LinearLayout>

子元素代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_selector"
    android:orientation="horizontal"
    >


    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >

        <ImageView
            android:id="@+id/list_image"
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:src="@drawable/ic_launcher" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/App_Name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignTop="@+id/list_image"
                android:layout_toRightOf="@+id/list_image"
                android:scrollHorizontally="false"
                android:text="Rihanna Love the way lie"
                android:textColor="#040404"
                android:textSize="12dip"
                android:textStyle="bold"
                android:typeface="serif" />

            <TextView
                android:id="@+id/App_Desc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/App_Name"
                android:layout_below="@+id/App_Name"
                android:scrollHorizontally="false"
                android:text="Just gona stand there and ..."
                android:textColor="#343434"
                android:textSize="10dip"
                android:typeface="monospace" />
        </LinearLayout>
            <ImageView
        android:id="@+id/Price"
        android:layout_width="20dip"
        android:layout_height="60dip"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/buy_gas" />

     </LinearLayout>


</RelativeLayout>

适配器代码

import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class BinderData extends BaseAdapter {

    // XML node keys
    static final String KEY_TAG = "weatherdata"; // parent node
    static final String KEY_ID = "id";
    static final String KEY_CITY = "city";
    static final String KEY_TEMP_C = "tempc";
    static final String KEY_TEMP_F = "tempf";
    static final String KEY_CONDN = "condition";
    static final String KEY_SPEED = "windspeed";
    static final String KEY_ICON = "icon";

    LayoutInflater inflater;
    ImageView thumb_image;
    List<HashMap<String,String>> weatherDataCollection;
    ViewHolder holder;
    public BinderData() {
        // TODO Auto-generated constructor stub
    }

    public BinderData(Activity act, List<HashMap<String,String>> map) {

        this.weatherDataCollection = map;

        inflater = (LayoutInflater) act
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


    public int getCount() {
        // TODO Auto-generated method stub
//      return idlist.size();
        return weatherDataCollection.size();
    }

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

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

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

        View vi=convertView;
        if(convertView==null){

          vi = inflater.inflate(R.layout.list_row, null);
          holder = new ViewHolder();

          holder.tvCity = (TextView)vi.findViewById(R.id.App_Name); // city name
          holder.tvWeather = (TextView)vi.findViewById(R.id.App_Desc); // city weather overview
          holder.tvTemperature =  (ImageView)vi.findViewById(R.id.Price); // city temperature
          holder.tvWeatherImage =(ImageView)vi.findViewById(R.id.list_image); // thumb image

          vi.setTag(holder);
        }
        else{

            holder = (ViewHolder)vi.getTag();
        }

          // Setting all values in listview

          holder.tvCity.setText(weatherDataCollection.get(position).get("App_Name"));
          holder.tvWeather.setText(weatherDataCollection.get(position).get("App_Desc"));
          String uri = "";
          if(weatherDataCollection.get(position).get("Price").equalsIgnoreCase("pro"))
          {
              uri = "drawable/label";

          }else{
              uri = "drawable/free";
          }
          int imageResource = vi.getContext().getApplicationContext().getResources().getIdentifier(uri, null, vi.getContext().getApplicationContext().getPackageName());
          Drawable image = vi.getContext().getResources().getDrawable(imageResource);
          holder.tvTemperature.setImageDrawable(image);
          //holder.tvTemperature.setText(weatherDataCollection.get(position).get("Price"));

          //Setting an image
          String uri2 = "drawable/"+ weatherDataCollection.get(position).get("image_url");
          int imageResource2 = vi.getContext().getApplicationContext().getResources().getIdentifier(uri2, null, vi.getContext().getApplicationContext().getPackageName());
          Drawable image2 = vi.getContext().getResources().getDrawable(imageResource2);
          holder.tvWeatherImage.setImageDrawable(image2);

          return vi;
    }

    /*
     * 
     * */
    static class ViewHolder{

        TextView tvCity;
        ImageView tvTemperature;
        TextView tvWeather;
        ImageView tvWeatherImage;
    }

}

列表视图实施:

  BinderData bindingData = new BinderData(this,songsList);
                list = (ListView) findViewById(R.id.list);

                list.setAdapter(bindingData);

请帮助我们..

2 个答案:

答案 0 :(得分:0)

试试这个子元素代码。它对我有用!

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_selector"
    android:orientation="horizontal"
    >


    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/list_image"
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:src="@drawable/ic_launcher" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            android:layout_marginLeft="5dp" 
            android:gravity="center">

            <TextView
                android:id="@+id/App_Name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scrollHorizontally="false"
                android:text="Rihanna Love the way lie"
                android:textColor="#040404"
                android:textSize="12dip"
                android:textStyle="bold"
                android:typeface="serif" 
                android:layout_gravity="center|left"/>

            <TextView
                android:id="@+id/App_Desc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scrollHorizontally="false"
                android:text="Just gona stand there and ..."
                android:textColor="#343434"
                android:textSize="10dip"
                android:typeface="monospace"
                android:layout_gravity="center|left"
                android:layout_marginTop="5dp" />
        </LinearLayout>
            <ImageView
        android:id="@+id/Price"
        android:layout_width="20dip"
        android:layout_height="60dip"
        android:src="@drawable/buy_gas" />

     </LinearLayout>

</RelativeLayout>

答案 1 :(得分:0)

求助:

问题在于最右边的图标,它具有非常高的分辨率,只是降低了分辨率并替换了文件并且工作正常。

感谢每个人的时间..