0

我创建了一个使用自定义列表视图片段的应用程序。但自定义列表视图显示不正确。

下面附上我的屏幕截图。

在此处输入图像描述

这是我的单个行项的 Xml 文件

<?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:orientation="vertical" >

<TextView
    android:id="@+id/Jobtitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="7dp"
    android:paddingRight="7dp"
    android:paddingTop="2dp"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="@color/green"
    android:textStyle="bold" >
</TextView>

<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/Jobtitle"
    android:orientation="horizontal"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="5dp" 
    android:paddingBottom="5dp">

    <TextView
        android:id="@+id/publishdate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="PubDate:-"
        android:textColor="@color/textgrey"
        />

    <TextView
        android:id="@+id/lefttime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/publishdate"
        android:layout_alignBottom="@+id/publishdate"
        android:layout_alignParentRight="true"
        android:text="Time Left:-"
        android:textColor="@color/textgrey"
         />
  </RelativeLayout>

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/relativeLayout1"
    android:background="@drawable/dividr" >
 </LinearLayout>

这是列表片段布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

    <ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
  >
</ListView>

<TextView
    android:id="@android:id/empty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="No text" >

</TextView>

这是我的适配器

 package my.newapp.freelanceeye;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

public class joblisted extends ListFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        adapt my=new adapt(getActivity(), R.layout.listrow,m);
        setListAdapter(my);
    }

    Context ctx;
    public class adapt extends ArrayAdapter<String>{
       @Override
        public View getView(int position, View convertView, ViewGroup parent) {
           LayoutInflater inflater=(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           View row=inflater.inflate(R.layout.listrow,parent,false);
           return row;
            //return super.getView(position, convertView, parent);

        }

        public adapt(Context context, int textViewResourceId, String[] objects) {
            super(context, textViewResourceId, objects);
            ctx=context;


        }


    }
    String[] m={"as","asa","sd","Ad","sad","asda"};

    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View v=inflater.inflate(R.layout.listfragmnetlayout,container,false);
            return v;

            }


    }
4

4 回答 4

1

您应该像这样设置片段的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

    <ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</<LinearLayout>

我认为问题在于您的视图高度,这就是为什么尝试使用match_parent.

于 2013-04-15T13:37:45.530 回答
0

您正在构建自定义数组适配器,但您没有在行字段中设置参数。

看一下这个适配器的实现,每一步都有注释解释:

private class CustomArrayAdapter extends ArrayAdapter<RowData>
{   
private ArrayList<RowData> list;

//this custom adapter receives an ArrayList of RowData objects.
//RowData is my class that represents the data for a single row and could be anything.
public CustomArrayAdapter(Context context, int textViewResourceId, ArrayList<RowData> rowDataList) 
{
    //populate the local list with data.
    super(context, textViewResourceId, rowDataList);
    this.list = new ArrayList<RowData>();
    this.list.addAll(rowDataList);
}

public View getView(final int position, View convertView, ViewGroup parent)
{
    //creating the ViewHolder we defined earlier.
    ViewHolder holder = new ViewHolder();) 

    //creating LayoutInflator for inflating the row layout.
    LayoutInflater inflator = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    //inflating the row layout we defined earlier.
    convertView = inflator.inflate(R.layout.row_item_layout, null);

    //setting the views into the ViewHolder.
    holder.title = (TextView) convertView.findViewById(R.id.tvItemTitle);
    holder.changeRowStatus = (ImageView) convertView.findViewById(R.id.iStatus);
    holder.changeRowStatus.setTag(position);

    //define an onClickListener for the ImageView.
    holder.changeRowStatus.setOnClickListener(new OnClickListener() 
    {           
        @Override
        public void onClick(View v) 
        {
            Toast.makeText(activity, "Image from row " + position + " was pressed", Toast.LENGTH_LONG).show();
        }
    });
    holder.checked = (CheckBox) convertView.findViewById(R.id.cbCheckListItem);
    holder.checked.setTag(position);

    //define an onClickListener for the CheckBox.
    holder.checked.setOnClickListener(new OnClickListener() 
    {       
        @Override
        public void onClick(View v)
        {
            //assign check-box state to the corresponding object in list.    
            CheckBox checkbox = (CheckBox) v;
            rowDataList.get(position).setChecked(checkbox.isChecked());
            Toast.makeText(activity, "CheckBox from row " + position + " was checked", Toast.LENGTH_LONG).show();    
        }
    });

    //setting data into the the ViewHolder.
    holder.title.setText(RowData.getName());
    holder.checked.setChecked(RowData.isChecked());

    //return the row view.
    return convertView;
}
}
于 2013-04-15T13:33:13.530 回答
0
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

Change layout_height to fill_parent

于 2014-04-06T06:09:51.840 回答
0
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

        <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Large Text"
            android:textColor="#339966"
            android:textAppearance="?android:attr/textAppearanceLarge" 
            android:textStyle="bold" />

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

            <TextView
                android:id="@+id/text2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                  android:text="PubDate:-"
                android:textColor="#606060"
                 />

            <TextView
                android:id="@+id/text3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Time Left:-"
                android:textColor="#606060"
                android:paddingLeft="180dp"
               />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

尝试这个....

于 2013-04-15T14:00:51.157 回答