ListView是有addHeaderView和 addFooterView兩個(gè)方法的.
烏拉特后ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!
但是作為官方推薦的ListView的升級(jí)版RecyclerView缺無法實(shí)現(xiàn)這兩個(gè)方法。
那么如果使用RecyclerView實(shí)現(xiàn)這兩個(gè)方法的效果該怎么做呢?
網(wǎng)上查詢了很久,試過各種各樣的實(shí)現(xiàn)方式,終于讓我發(fā)現(xiàn)一個(gè)還不錯(cuò)的實(shí)現(xiàn)方法,那么就給大家推薦一下。
項(xiàng)目地址(別人寫的,非博主的)
我看了下這個(gè)源碼,很簡單,即寫了一個(gè)繼承RecyclerView的控件,自己實(shí)現(xiàn)addHeaderView和addFooterView兩個(gè)方法
?
code
package com.xqx.com.recyclerviewheaderdemo;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
public class XCRecyclerView extends RecyclerView{
private ArrayListView mHeaderViews = new ArrayList();
private ArrayListView mFooterViews = new ArrayList();
private RecyclerView.Adapter mAdapter;
private RecyclerView.Adapter mWrapAdapter;
private static final int TYPE_HEADER = -101;
private static final int TYPE_FOOTER = -102;
private static final int TYPE_LIST_ITEM = - 103;
public XCRecyclerView(Context context) {
this(context, null);
}
public XCRecyclerView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public XCRecyclerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context context){
}
@Override
public void setAdapter(Adapter adapter) {
mAdapter = adapter;
mWrapAdapter = new WrapAdapter(mHeaderViews, mFooterViews, adapter);
super.setAdapter(mWrapAdapter);
mAdapter.registerAdapterDataObserver(mDataObserver);
}
public void addHeaderView(View view){
mHeaderViews.clear();
mHeaderViews.add(view);
}
public void addFooterView(View view){
mFooterViews.clear();
mFooterViews.add(view);
}
public int getHeaderViewsCount(){
return mHeaderViews.size();
}
public int getFooterViewsCount(){
return mFooterViews.size();
}
private final RecyclerView.AdapterDataObserver mDataObserver = new RecyclerView.AdapterDataObserver() {
@Override
public void onChanged() {
mWrapAdapter.notifyDataSetChanged();
}
@Override
public void onItemRangeChanged(int positionStart, int itemCount) {
mWrapAdapter.notifyItemRangeChanged(positionStart, itemCount);
}
// @Override
// public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
// mWrapAdapter.notifyItemRangeChanged(positionStart, itemCount, payload);
// }
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
mWrapAdapter.notifyItemRangeInserted(positionStart, itemCount);
}
@Override
public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
mWrapAdapter.notifyItemMoved(fromPosition, toPosition);
}
@Override
public void onItemRangeRemoved(int positionStart, int itemCount) {
mWrapAdapter.notifyItemRangeRemoved(positionStart, itemCount);
}
};
private class WrapAdapter extends RecyclerView.AdapterViewHolder{
private Adapter mAdapter;
private ListView mHeaderViews;
private ListView mFooterViews;
public WrapAdapter(ListView headerViews,ListView footerViews,Adapter adapter){
this.mAdapter = adapter;
this.mHeaderViews = headerViews;
this.mFooterViews = footerViews;
}
public int getHeaderCount(){
return this.mHeaderViews.size();
}
public int getFooterCount(){
return this.mFooterViews.size();
}
public boolean isHeader(int position){
return position = 0 position this.mHeaderViews.size();
}
public boolean isFooter(int position){
return position getItemCount() position = getItemCount() - this.mFooterViews.size();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType == TYPE_HEADER){
return new CustomViewHolder(this.mHeaderViews.get(0));
}else if(viewType == TYPE_FOOTER){
return new CustomViewHolder(this.mFooterViews.get(0));
}else{
return this.mAdapter.onCreateViewHolder(parent,viewType);
}
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
if(isHeader(position)) return;
if(isFooter(position)) return;
int rePosition = position - getHeaderCount();
int itemCount = this.mAdapter.getItemCount();
if(this.mAdapter != null){
if(rePosition itemCount){
Log.v("czm","rePosition/itemCount="+rePosition+"/"+itemCount);
this.mAdapter.onBindViewHolder(holder,rePosition);
return;
}
}
}
@Override
public long getItemId(int position) {
if (this.mAdapter != null position = getHeaderCount()) {
int rePosition = position - getHeaderCount();
int itemCount = this.mAdapter.getItemCount();
if (rePosition itemCount) {
return this.mAdapter.getItemId(rePosition);
}
}
return -1;
}
@Override
public int getItemViewType(int position) {
if(isHeader(position)){
return TYPE_HEADER;
}
if(isFooter(position)){
return TYPE_FOOTER;
}
int rePosition = position - getHeaderCount();
int itemCount = this.mAdapter.getItemCount();
if(rePosition itemCount){
return this.mAdapter.getItemViewType(position);
}
return TYPE_LIST_ITEM;
}
@Override
public int getItemCount() {
if(this.mAdapter != null){
return getHeaderCount() + getFooterCount() + this.mAdapter.getItemCount();
}else{
return getHeaderCount() + getFooterCount();
}
}
@Override
public void registerAdapterDataObserver(AdapterDataObserver observer) {
if(this.mAdapter != null){
this.mAdapter.registerAdapterDataObserver(observer);
}
}
@Override
public void unregisterAdapterDataObserver(AdapterDataObserver observer) {
if(this.mAdapter != null){
this.mAdapter.unregisterAdapterDataObserver(observer);
}
}
private class CustomViewHolder extends ViewHolder{
public CustomViewHolder(View itemView) {
super(itemView);
}
}
}
}
XCRecyclerView
使用方法github里也寫的清清楚楚的
?
code
private MyAdapter mAdapter;
private XCRecyclerView mRecyclerView;
private ListString mData;
private View mHeaderView;
private View mFooterView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mData = new ArrayListString();
for(int i = 0; i 10 ;i++){
mData.add("item_" + i);
}
mAdapter = new MyAdapter(mData);
mRecyclerView = (XCRecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mHeaderView = LayoutInflater.from(this).inflate(R.layout.layout_header,mRecyclerView,false);
mFooterView = LayoutInflater.from(this).inflate(R.layout.layout_footer,mRecyclerView,false);
mRecyclerView.addHeaderView(mHeaderView);
mRecyclerView.addFooterView(mFooterView);
mRecyclerView.setAdapter(mAdapter);
}
注意點(diǎn):
addHeaderView之后 列表的數(shù)據(jù)坐標(biāo)即相應(yīng)發(fā)生變化!即addHeadView一次,列表第一個(gè)數(shù)據(jù)的下坐標(biāo)+1(0--1)
一般Web瀏覽器在訪問各個(gè)網(wǎng)站時(shí),會(huì)在HTTP請(qǐng)求的Header里設(shè)置User-Agent字段,表示請(qǐng)求者的身份信息,比如在我華為U9200的Android手機(jī)上,打開Chrome瀏覽器后,每個(gè)訪問請(qǐng)求的User-Agent字段如下:
?
Mozilla/5.0 (Linux; Android 4.0.3; U9200 Build/HuaweiU9200)
對(duì)于安卓App來說,默認(rèn)情況下的HTTP請(qǐng)求Header是不會(huì)帶上這么一串字符串的。如果要帶必須得自己構(gòu)造了,我摸索了半天,還是找到了達(dá)到和瀏覽器同樣格式的設(shè)置User-Agent的方法:
?
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
// HttpPost httpPost = new HttpPost(url); // 如果是POST請(qǐng)求,則改為注釋掉上面一行
// MY_APP_NAME為APP名稱,MY_APP_VERSION_NAME為應(yīng)用的版本名
httpGet.setHeader("User-Agent", String.format("%s/%s (Linux; Android %s; %s Build/%s)", MY_APP_NAME, MY_APP_VERSION_NAME, Build.VERSION.RELEASE, Build.MANUFACTURER, Build.ID));
第一種:點(diǎn)擊File - Settings - Editor - File and Code Templates
第二種:點(diǎn)擊File - Settings - Editor - Live Template
這樣當(dāng)新建一個(gè)類或者方法后,在類或方法名上面輸入縮寫(cc或cm),就會(huì)有提示是否添加類注釋。
Shift+Alt+G 添加注釋 Shift+Alt+Z 取消注釋
一頭叫USB一頭叫TYPEC。普通安卓接口就叫做安卓接口也叫做USB連接線,安卓頭指的是手機(jī)安卓系統(tǒng)的充電插頭的意思。手機(jī)充電器大致可以分為旅行充電器、座式充電器和維護(hù)型充電器,一般用戶接觸的主要是前面兩種。而市場上賣得最多的是旅行充電器。
安卓充電功率
手機(jī)常用鋰離子電池的充電器采用的是恒流限壓充電制,充電電流一般采用C2左右即采用兩小時(shí)充電率,比如500mAh電池采用250mA充電大約兩小時(shí)達(dá)到42V后再恒壓充電。
lion電池并不適合采用NIMH電池高級(jí)快速充電器所用的-DV/DT檢測快速充電方式,因?yàn)閘ion電池對(duì)充電電流有嚴(yán)格的限制.鋰離子非?;顫?,大電流充電很容易產(chǎn)生危險(xiǎn)。
1.目標(biāo)組件(button)有兩套,放在頂部和內(nèi)容中間;
2.當(dāng)內(nèi)容中間的組件滑動(dòng)至頂部欄位置時(shí)控制顯示/隱藏頂部和中間的組件(涉及到組件獲取在屏幕的位置知識(shí)點(diǎn));
activity代碼:
public class MainActivity extends AppCompatActivity implements ObservableScrollView.ScrollViewListener {
private ObservableScrollView scrollView;
private Button topBtn1, topBtn2, middleBtn1, middleBtn2;
private View topPanel, middlePanel;
private int topHeight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
initListeners();
①利用代碼去除(速度慢,但靈活)
requestWindowFeature(Window.FEATURE_NO_TITLE);
②在清單文件中聲明(速度快,但不靈活)
android:theme="@android:style/Theme.Light.NoTitleBar(背景是白色)
android:theme="@android:style/Theme.Translucent.NoTitleBar"(完全透明的)