我自己解決了這個問題。
獲得與圖像中提到的相同的外觀和感覺。
我做了以下:
1)定義於ListView中使用3個佈局。
list_top_one - >對於具有從日期和到日期文字視圖
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/frmdate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="from date"/>
<TextView
android:id="@+id/todate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="to date"/>
</LinearLayout>
b)中list_item_in_middle頂視圖 - >爲具有一樣的感覺
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:id="@+id/llHeader">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/btn_background"
android:text="OK"
android:padding="16dp"/>
</LinearLayout>
c按鈕)list_items - 用於保持>動態加載的值。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFBB00"
android:orientation="vertical" >
<TextView
android:textColor="#000"
android:layout_margin="30dp"
android:id="@+id/button1"
android:layout_gravity="center"
android:text="OK"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
2)主要活動佈局。
A)含有一個列表視圖
b)中,並且包括一個佈局將被示爲按鈕。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
<!-- This LinearLayout's visibility is toggled -->
<include layout="@layout/list_item_in_middle" />
</RelativeLayout>
參考: Making a middle element to get stuck in the header (ScrollView/ListView)
3)ListAdapter類
public class ListAdapter extends BaseAdapter {
public Context mContext;
TextView btCurrent;
ArrayList<Integer> newarrays = new ArrayList<>();
ListAdapter(Context context,Integer[] arrval) {
mContext = context;
newarrays.addAll(Arrays.asList(arrval));
}
public void InitializeValues(Integer[] arrval){
newarrays.addAll(Arrays.asList(arrval));
}
@Override
public int getCount() {
return newarrays.size();
}
@Override
public Object getItem(int arg0) {
return newarrays.get(arg0);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(position == 0){
convertView = inflater.inflate(R.layout.list_item_in_middle, null);//Adding btn layout
}else {
convertView = inflater.inflate(R.layout.list_items, null);// Adding other elements
btCurrent = (TextView) convertView.findViewById(R.id.button1);
if ((Integer) getItem(position) == 3) {
btCurrent.setText("Number " + getItem(position) + " is sticky");
} else {
btCurrent.setText("" + getItem(position));
}
}
return convertView;
}
}
4)MainActivity
一個)OnScrollListener用於安裝BTN像在上面的列表項。
llHeader = (LinearLayout) findViewById(R.id.llHeader);//from the list_item_in_middle layout
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (firstVisibleItem > 0) { // 1st row will stick
llHeader.setVisibility(View.VISIBLE);
} else {
llHeader.setVisibility(View.GONE);
}
}
});
B)的ListView
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int a = 0;
if(position == 0){//for clicking from date and to date.
TextView frm = (TextView) view.findViewById(R.id.frmdate);
frm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"From date clicked",Toast.LENGTH_SHORT).show();
}
});
TextView to = (TextView) view.findViewById(R.id.todate);
to.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"to date clicked",Toast.LENGTH_SHORT).show();
}
});
}else {
if(position == 1) {//for clicking the btn like list element.
swap();
Toast.makeText(MainActivity.this,"Header clicked",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this, parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
}
}
}
});
C)的OnItemClickListener LinearLayout中單擊函數,該函數被卡住,從LinearLayout中堪稱頂級
llHeader.setOnClickListener(new View.OnClickListener() { //for clicking the elements that gets stuck to the top
@Override
public void onClick(View v) {
swap();
Toast.makeText(MainActivity.this,"Header clicked",Toast.LENGTH_SHORT).show();
}
});
d)交換功能和列表元素元素在位置1
public void swap(){
ltAdapter.notifyDataSetChanged();
new1 = new Integer[]{20, 21, 22, 23, 24, 25, 26, 27, 28};
ltAdapter.InitializeValues(new1);
}
注:如果有任何可獲得焦點視圖將在列表視圖中,OnItemclickListener亙古不變的工作,在計算器閱讀本身
最後輸出:
第二元素之前點擊
點擊 後
滾動後
張貼您的xml也 –
[檢查](http://cyrilmottier.com/2014/05/20/custom-animations-with-fragments/) – Skynet
@Skynet。是的,你分享的鏈接正是我正在尋找的。請給我一些代碼幫助,我可以使用它來實現這種效果 – SimplyProgrammer