我在Android应用程序中需要一些帮助。我在scrollview中有2个listviews。我希望我的listviews不可滚动查看我的所有listview项目。所以这是我的代码列出所有项目:
public class CustomList extends ListView
{
boolean expanded = false;
public CustomList(Context context)
{
super(context);
}
public CustomList(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public CustomList(Context context, AttributeSet attrs,int defStyle)
{
super(context, attrs, defStyle);
}
public boolean isExpanded()
{
return expanded;
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
// HACK! TAKE THAT ANDROID!
if (isExpanded())
{
// Calculate entire height by providing a very large height hint.
// But do not use the highest 2 bits of this integer; those are
// reserved for the MeasureSpec mode.
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
else
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public void setExpanded(boolean expanded)
{
this.expanded = expanded;
}
}
这是我的.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scroll"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:layout_width="match_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
tools:context="com.example.cagri.mobilyateknikservis.BildirimActivity"
android:orientation="vertical"
android:id="@+id/activity_bildirim">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Müşteri:"
android:background="@android:color/background_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/MusteriAdi"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:textColor="@android:color/holo_red_dark"
android:textStyle="normal|bold"
android:layout_weight="1"
android:gravity="center_vertical" />
<TextView
android:text="TextView"
android:background="@android:color/background_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtMusteri"
android:layout_weight="1"
android:textSize="12sp"
android:textColor="@android:color/background_dark"
android:textAlignment="textStart" />
<TextView
android:text="Hizmet Tipi:"
android:background="@android:color/background_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/HizmetTipi"
android:textColor="@android:color/holo_red_dark"
android:layout_alignParentTop="true"
android:textStyle="normal|bold"
android:layout_weight="1" />
<TextView
android:text="TextView"
android:background="@android:color/background_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtHizmetTipi"
android:layout_weight="1"
android:textSize="12sp"
android:textColor="@android:color/background_dark"
android:textAlignment="textStart" />
<TextView
android:text="Sipariş Ref. No."
android:background="@android:color/background_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/SiparisRef"
android:textColor="@android:color/holo_red_dark"
android:layout_alignParentTop="true"
android:textStyle="normal|bold"
android:layout_weight="1" />
<TextView
android:text="TextView"
android:background="@android:color/background_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtSiparisRef"
android:layout_weight="1"
android:textSize="12sp"
android:textColor="@android:color/background_dark"
android:textAlignment="textStart" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Firma:"
android:background="@android:color/background_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Firma"
android:layout_weight="1"
android:textColor="@android:color/holo_red_dark"
android:textStyle="normal|bold" />
<TextView
android:text="Firma"
android:background="@android:color/background_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtFirma"
android:layout_weight="2"
android:textColor="@android:color/background_dark"
android:textSize="12sp"
android:textAlignment="viewStart" />
<TextView
android:text="Sipariş Tarihi:"
android:background="@android:color/background_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/SiparisTarihi"
android:layout_weight="1"
android:textColor="@android:color/holo_red_dark"
android:textStyle="normal|bold" />
<TextView
android:text="Sipariş Tarihi"
android:background="@android:color/background_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtSiparisTarihi"
android:layout_weight="2"
android:textColor="@android:color/background_dark"
android:textSize="12sp"
android:textAlignment="viewStart" />
<TextView
android:text="Teslim Tarihi:"
android:background="@android:color/background_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/TeslimTarihi"
android:layout_weight="1"
android:textColor="@android:color/holo_red_dark"
android:textStyle="normal|bold" />
<TextView
android:text="Teslim Tarihi"
android:background="@android:color/background_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtTeslimTarihi"
android:layout_weight="2"
android:textColor="@android:color/background_dark"
android:textSize="12sp"
android:textAlignment="viewStart" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Depo:"
android:background="@android:color/background_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Depo"
android:layout_weight="1"
android:textColor="@android:color/holo_red_dark"
android:textStyle="normal|bold" />
<TextView
android:text="Depo"
android:background="@android:color/background_light"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/txtDepo"
android:layout_weight="0.98"
android:textColor="@android:color/background_dark"
android:textSize="12sp"
android:gravity="center_vertical" />
<TextView
android:text="Telefon:"
android:background="@android:color/background_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Telefon"
android:layout_weight="1"
android:textStyle="normal|bold"
android:textColor="@android:color/holo_red_dark"
android:textAlignment="viewStart" />
<TextView
android:text="Telefon"
android:background="@android:color/background_light"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/txtTelefon"
android:layout_weight="8.5"
android:textSize="12sp"
android:textColor="@android:color/background_dark"
android:gravity="center_vertical" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Adres:"
android:background="@android:color/background_light"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/Adres"
android:layout_weight="0.99"
android:textStyle="normal|bold"
android:textColor="@android:color/holo_red_dark"
android:textAllCaps="false"
android:gravity="center_vertical" />
<TextView
android:text="Adres"
android:background="@android:color/background_light"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/txtAdres"
android:layout_weight="14.01"
android:textSize="12sp"
android:textColor="@android:color/background_dark"
android:gravity="center_vertical" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Açıklama:"
android:background="@android:color/background_light"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/Aciklama"
android:layout_weight="1"
android:textStyle="normal|bold"
android:textColor="@android:color/holo_red_dark"
android:gravity="center_vertical" />
<TextView
android:text="Açıklama"
android:background="@android:color/background_light"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/txtAciklama"
android:layout_weight="9"
android:textColor="@android:color/background_dark"
android:textSize="12sp"
android:gravity="center_vertical" />
</LinearLayout>
<TextView
android:text="İŞLEMLER"
android:layout_width="match_parent"
android:gravity="center_vertical"
android:id="@+id/Islemler"
android:textAlignment="center"
android:background="@android:color/white"
android:textColor="@color/colorAccent"
android:layout_height="30dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_height="match_parent">
<TextView
android:text="İşlem"
android:background="@android:color/background_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Islem"
android:layout_weight="1"
android:textAlignment="textStart"
android:textColor="@android:color/holo_red_dark"
android:maxWidth="100dp"
android:minWidth="100dp"
android:width="100dp" />
<TextView
android:text="Açıklama"
android:background="@android:color/background_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Aciklama2"
android:layout_weight="4"
android:textAlignment="textStart"
android:textColor="@android:color/holo_red_dark"
android:minWidth="180dp"
android:width="180dp"
android:maxWidth="180dp" />
<TextView
android:text="Sonuç"
android:background="@android:color/background_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Sonuc"
android:layout_weight="1.7"
android:textAlignment="textStart"
android:textColor="@android:color/holo_red_dark" />
<TextView
android:text="Sonuç Tarihi"
android:background="@android:color/background_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/SonucTarihi"
android:layout_weight="1"
android:textAlignment="center"
android:textColor="@android:color/holo_red_dark" />
</LinearLayout>
<com.example.cagri.mobilyateknikservis.CustomList
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:listSelector="@android:color/white"
android:background="@android:color/white"
android:id="@+id/lstIsl"
android:layout_weight="1"
android:fadingEdge="none"
android:focusable="false"
android:scrollbars="none" />
<TextView
android:text="PARÇALAR"
android:layout_width="match_parent"
android:id="@+id/Parcalar"
android:gravity="center_vertical"
android:textAlignment="center"
android:background="@android:color/white"
android:textColor="@color/colorAccent"
android:layout_height="30dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Talep Tarihi"
android:background="@android:color/background_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView8"
android:textColor="@android:color/holo_red_dark"
android:textAlignment="textStart" />
<TextView
android:text="Parça"
android:background="@android:color/background_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView7"
android:layout_weight="1"
android:textAlignment="textStart"
android:textColor="@android:color/holo_red_dark" />
<TextView
android:text="Miktar"
android:background="@android:color/background_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView6"
android:layout_weight="1"
android:textAlignment="textStart"
android:textColor="@android:color/holo_red_dark" />
<TextView
android:text="İade Durumu"
android:background="@android:color/background_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView5"
android:layout_weight="1"
android:textColor="@android:color/holo_red_dark"
android:textAlignment="textStart" />
<TextView
android:text="Açıklama"
android:background="@android:color/background_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView4"
android:layout_weight="1"
android:textAlignment="textStart"
android:textColor="@android:color/holo_red_dark" />
<TextView
android:text="Sonuç"
android:background="@android:color/background_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView3"
android:layout_weight="1"
android:textAlignment="textStart"
android:textColor="@android:color/holo_red_dark" />
<TextView
android:text="Sonuç Tarihi"
android:background="@android:color/background_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:layout_weight="1"
android:textAlignment="textStart"
android:textColor="@android:color/holo_red_dark" />
</LinearLayout>
<com.example.cagri.mobilyateknikservis.CustomList
android:layout_width="match_parent"
android:layout_height="0dp"
android:listSelector="@android:color/white"
android:background="@android:color/white"
android:id="@+id/lstParcalar"
android:fadingEdge="none"
android:focusable="false"
android:scrollbars="none" />
</LinearLayout>
</ScrollView>
我的自定义列表视图适配器:
public class ParcaAdapter extends BaseAdapter {
private LayoutInflater inflater;
private List<Parcalar> parcaBilgileri;
public ParcaAdapter(Activity activity, List<Parcalar> parca)
{
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
parcaBilgileri = parca;
}
public int getCount() {
return parcaBilgileri.size();
}
public Parcalar getItem(int position) {
return parcaBilgileri.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View satirView;
satirView = inflater.inflate(R.layout.parcalar_list, null);
TextView txtTalepTarihi = (TextView) satirView.findViewById(R.id.txtTalepTarihi);
TextView txtParca = (TextView) satirView.findViewById(R.id.txtParca);
TextView txtMiktar = (TextView) satirView.findViewById(R.id.txtMiktar);
TextView txtIadeDurumu = (TextView) satirView.findViewById(R.id.txtIadeDurumu);
TextView txtAciklama = (TextView) satirView.findViewById(R.id.txtAciklama);
TextView txtSonuc = (TextView) satirView.findViewById(R.id.txtSonuc);
TextView txtSonucTarihi = (TextView) satirView.findViewById(R.id.txtSonucTarihi);
Parcalar bilgi = parcaBilgileri.get(position);
txtTalepTarihi.setText(bilgi.gettaleptarihi());
txtParca.setText(bilgi.getparca());
txtMiktar.setText(bilgi.getmiktar());
txtIadeDurumu.setText(bilgi.getiadedurumu());
txtAciklama.setText(bilgi.getaciklama());
txtSonuc.setText(bilgi.getsonuc());
txtSonucTarihi.setText(bilgi.getsonuctarihi());
return satirView;
}
}
感谢您的帮助。
答案 0 :(得分:1)
使用RecyclerView而不是ListView,而对于ScrollView则使用NestedScrollView。它专为这种滚动而设计。另一件事,让recyclerView高度为白色空间的wrap_content
答案 1 :(得分:0)
无需为此完成所有代码。您只需根据子视图更改列表视图高度。只需使用简单的适配器,然后在setAdapter调用下方法之后。
private void setListViewHeightBasedOnChildren(ListView listView, int columns, int arrayPosition) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
boolean isSaved = false;
int totalHeight = 0;
int tempHeight = 10;
int items = listAdapter.getCount();
int rows = 0;
View listItem = listAdapter.getView(0, null, listView);
listItem.measure(0, 0);
totalHeight = listItem.getMeasuredHeight();
float x = 1;
if (items > columns) {
x = items / columns;
if (arrayPosition == 0) {
rows = (int) (x + 1);
} else {
rows = (int) (x + 3);
}
totalHeight *= rows;
if (!isSaved) {
tempHeight += rows;
isSaved = true;
}
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (tempHeight * 4);
listView.setLayoutParams(params);
}
根据需要修改params.height。
像这样称呼这个方法:
setListViewHeightBasedOnChildren(yourListView, 1, position);