我正在尝试在android中实现表格布局。我在一个xml文件中设置了表格标题和行,并使用适配器传递数据。所发生的事情是我横向滚动但每行重复多个表格标题。 我想要实现的是表头只应该只看一次,它下面的所有行都应该与表头一起滚动。
这是我的xml文件
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView android:id="@+id/horizontalView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:scrollbars="horizontal|vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left"
android:stretchColumns="*"
android:divider="@color/black"
android:showDividers="middle"
>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Name"
android:textAllCaps="false"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Type"
android:textAllCaps="false"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Fare"
android:textAllCaps="false"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Tax"
android:textAllCaps="false"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Airline Pnr"
android:textAllCaps="false"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Ticekt Numner"
android:textAllCaps="false"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Cancel"
android:textAllCaps="false"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="18sp"
android:id="@+id/name"/>
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="@+id/type"
android:textColor="@color/black"
android:textSize="18sp" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="@+id/fare"
android:textColor="@color/black"
android:textSize="18sp" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="@+id/tax"
android:textColor="@color/black"
android:textSize="18sp" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="@+id/air_pnr"
android:textColor="@color/black"
android:textSize="18sp" />
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:id="@+id/ticket"
android:textColor="@color/black"
android:textSize="18sp" />
<CheckBox
android:id="@+id/check_bx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CANCEL" />
</TableRow>
</TableLayout>
</HorizontalScrollView>
这是图像的样子scrolling table
更新(2)HERE的适配器代码
public class Flight_cancel_Adapter extends BaseAdapter {
Context context;
public static ArrayList<Flight_cancel_Details> rowItems;
public static int available_seats;
public static PolicyAdapter adapter;
int TOTAL;
public static ArrayList<String> arraySeat=new ArrayList<String>();
Flight_cancel_Adapter(Context context, ArrayList<Flight_cancel_Details> rowItems) {
this.context = context;
this.rowItems = rowItems;
}
@Override
public int getCount() {
return rowItems.size();
}
@Override
public Object getItem(int position) {
return rowItems.get(position);
}
@Override
public long getItemId(int position)
{
return rowItems.indexOf(getItem(position));
}
/* private view holder class */
private class ViewHolder {
TextView type;
TextView fare;
TextView tax;
TextView air_pnr;
TextView ticket;
TextView namee;
CheckBox box;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
final ViewHolder holder;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.test, null);
holder = new ViewHolder();
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.namee = (TextView) convertView.findViewById(R.id.name);
holder.type = (TextView) convertView.findViewById(R.id.type);
holder.fare = (TextView) convertView.findViewById(R.id.fare);
holder.tax = (TextView) convertView.findViewById(R.id.tax);
holder.air_pnr = (TextView) convertView.findViewById(R.id.air_pnr);
holder.ticket = (TextView) convertView.findViewById(R.id.ticket);
holder.box = (CheckBox) convertView.findViewById(R.id.check_bx);
holder.box.setTag(position);
try
{
final Flight_cancel_Details row_pos = rowItems.get(position);
String name=String.valueOf(row_pos.getName());
String typ=String.valueOf(row_pos.gettypes());
String far=String.valueOf(row_pos.getfares());
String tax=String.valueOf(row_pos.gettax());
String pnr=String.valueOf(row_pos.getair_pnr());
String ticket=String.valueOf(row_pos.getticket());
System.out.print("ROW POS-"+row_pos.getStatus());
if(row_pos.getStatus().equals("CANCELLED"))
{
holder.namee.setText(name);
holder.type.setText(typ);
holder.fare.setText(far);
holder.tax.setText(tax);
holder.air_pnr.setText(pnr);
holder.ticket.setText(ticket);
holder.box.setEnabled(false);
holder.box.setText("CANCELLED");
}else {
holder.namee.setText(name);
holder.type.setText(typ);
holder.fare.setText(far);
holder.tax.setText(tax);
holder.air_pnr.setText(pnr);
holder.ticket.setText(ticket);
holder.box.setEnabled(true);
holder.box.setText("CANCEL");
}
if(row_pos.getTarvel_Status().equals("TRAVELLED"))
{
holder.namee.setText(name);
holder.type.setText(typ);
holder.fare.setText(far);
holder.tax.setText(tax);
holder.air_pnr.setText(pnr);
holder.ticket.setText(ticket);
}
}
catch (Exception e)
{
Log.e("BUS_CANCEL_ADAP ERROR:", e.getMessage());
}
convertView.setTag(holder);
holder.box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
int position = (int) buttonView.getTag();
System.out.println("--CLICKED--" + position);
String contactId = (holder.air_pnr.getText().toString());
System.out.println("--VALUE--" + contactId);
arraySeat.add(contactId);
}
else
{
int position = (int) buttonView.getTag();
System.out.println("--UNCHEKED--" + position);
String contactId = (holder.air_pnr.getText().toString());
System.out.println("--UNCHEKED VALUE--" + contactId);
arraySeat.remove(contactId);
}
}
});
return convertView;
}
}
答案 0 :(得分:0)
在我的XML文件中,您应该在TableView
上为所有行LinearLayout
(TextView)添加标题。
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView android:id="@+id/horizontalView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:scrollbars="horizontal|vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--Title for all row, once for all row -->
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Name"
android:textAllCaps="false"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Type"
android:textAllCaps="false"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Fare"
android:textAllCaps="false"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Tax"
android:textAllCaps="false"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Airline Pnr"
android:textAllCaps="false"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Ticekt Numner"
android:textAllCaps="false"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Cancel"
android:textAllCaps="false"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
</LinearLayout>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:stretchColumns="*"
android:divider="@color/black"
android:showDividers="middle"
>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="My Name"
android:textColor="@color/black"
android:textSize="18sp"
android:id="@+id/name"/>
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="@+id/type"
android:text="My Type"
android:textColor="@color/black"
android:textSize="18sp" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="@+id/fare"
android:textColor="@color/black"
android:textSize="18sp" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="@+id/tax"
android:textColor="@color/black"
android:textSize="18sp" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="@+id/air_pnr"
android:textColor="@color/black"
android:textSize="18sp" />
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:id="@+id/ticket"
android:textColor="@color/black"
android:textSize="18sp" />
<CheckBox
android:id="@+id/check_bx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CANCEL" />
</TableRow>
</TableLayout>
</LinearLayout>
</HorizontalScrollView>
答案 1 :(得分:0)
使用Recyclerview进行水平滚动视图以获得更好的性能
test.xml
<?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">
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Name"
android:textAllCaps="false"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="name"
android:textColor="@color/black"
android:textSize="18sp"
android:id="@+id/name"/>
</LinearLayout>
recyclerview.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView android:id="@+id/horizontalView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:scrollbars="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:listitem="@layout/layout">
</android.support.v7.widget.RecyclerView>