[轉載]Android: 如何?ScrollView中含有ListView?

來源:互聯網
上載者:User

標籤:android   style   blog   http   ar   io   os   使用   sp   

原文地址:Android: 如何?ScrollView中含有ListView? mailofzxfListView本身就含有ScrollView,因此把ListView放到ScrollView中會引起混亂(誰來響應滑動事件?)但有時又確有此需求,以實現ListView的內容連同其他內容的滾動。

要想把ListView嵌入ScrollView,有二個方法:

方法1:整體上使用一個ListView, 把不需滾動的部分放入ListView的Header或Footer中。
注意: 一定要先添加HearderView和FooterView,然後再設定ListView的Adapter.
缺陷: Header或Footer中的內容不能滾動;並且只能允許同時存在一個ListView。
代碼如下:

     LayoutInflater mLI =
           (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

     listView = (ListView) findViewById(R.id.listView);
     datas = new ArrayList<Object>();
     fillDatas();
     datasAdapter = new DemoListAdapter(context, datas);
     MyListHeaderView headerView = mLI.inflate(R.layout.my_header_layout, null);
     FillDataToHeaderView(headerView);
     MyListFooterView footerView = mLI.inflate(R.layout.my_footer_layout, null);
     FillDataToFooterView(footerView); 

     listView.addHeaderView(headerView); 
     listView.addFooterView(footerView);
     listView.setAdapter(datasAdapter);
 
方法2:用LinearLayout類比ListView, 從而實現任意內容的滾動。
下面實現的是一個通用的LinearLayoutForListView,可以為它設定不同的Adapter從而顯示不同的資料。
LinearLayoutForListView的代碼如下:

public class LinearLayoutForListView extends android.widget.LinearLayout {
    static final String LOG_TAG = "LinearLayoutForListView";
    private android.widget.BaseAdapter adapter;
    private OnClickListener onClickListener = null;

    public void fillLinearLayout() {
        int count = adapter.getCount();
        for (int i = 0; i < count; i++) {
            View v = adapter.getView(i, null, null);
            v.setOnClickListener(this.onClickListener);
            addView(v, i);
        }
        Log.v("countTAG", "" + count);
    }

    public LinearLayoutForListView(Context context) {
        super(context);
    }

    public LinearLayoutForListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public android.widget.BaseAdapter getAdpater() {
        return adapter;
    }

    public void setAdapter(android.widget.BaseAdapter adpater) {
        this.adapter = adpater;
        fillLinearLayout();
    }

    public OnClickListener getOnclickListner() {
        return onClickListener;
    }

    public void setOnclickLinstener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }
}

注意,上面LinearLayoutForListView的成員adapter是BaseAdapter,因此可以自訂其它的Adapter來加入資料。下面是一個Adapter的參考實現,用於顯示一個軟體開發人員的聯絡資訊:

public class DeveloperInfoAdapter extends BaseAdapter  {
    static final String LOG_TAG="DeveloperInfo";
    public final int VIEW_INDEX = 3000;
    private Context  mContext;
    private String[] mKeys;
    private String[] mTitles;
    private String[] mVals;
    LayoutInflater mLI = null;

    public DeveloperInfoAdapter(Context context, String[] keys, String[] titles, String[] vals) {
        mContext = context;
        mKeys = keys;
        mTitles = titles;
        mVals = vals;
        mLI = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // to Make sure mKeys(...) are NOT null:
        if(mKeys==null || mTitles==null || mVals==null) {
            mKeys = mTitles = mVals = new String[]{};
        }
    }
    
    @Override
    public int getCount() {
        return mKeys.length;
    }
    
    @Override
    public String getItem(int index) {
        if(index>=0 && index<mVals.length)
            return mVals[index];
        else
            return null;
    }
    
    @Override
    public long getItemId(int position) {
        return position;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
            if(position <0 || position > mKeys.length)
                return null;
            final String key = mKeys[position];
            final String title = mTitles[position];
            final String val = mVals[position];
            
            View v = convertView;
            if (v == null) {
                v = mLI.inflate(R.layout.dev_web_email_layout, null);
            }
            v.setId(position+VIEW_INDEX);
            v.setClickable(true);
            
            if (true) {
                TextView v_title = (TextView) v.findViewById(R.id.dev_title);
                TextView v_val = (TextView) v.findViewById(R.id.dev_val);
                v_title.setText(title);
                v_val.setText(val);
            }
            return v;
    }

    
    private void callBrowser(String url){
        if(TextUtils.isEmpty(url))
            return;
        final String prefix = "http://";
        try {
            if(!url.startsWith(prefix))
                url = prefix + url;
            Uri uri = Uri.parse(url);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            mContext.startActivity(intent);
        } catch (Exception e) {
            String err_msg = e.getMessage();
            Log.d(LOG_TAG, "Uri err: " + err_msg);
        }
    }

    private void callEmail(String emailbox){
        if(TextUtils.isEmpty(emailbox))
            return;
        final String prefix = "mailto:";
        try {
            if(!emailbox.startsWith(prefix))
                emailbox = prefix + emailbox;
            Uri uri = Uri.parse(emailbox);
            Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
            mContext.startActivity(intent);
        } catch (Exception e) {
            String err_msg = e.getMessage();
            Log.d(LOG_TAG, "Uri err: " + err_msg);
        }
    }
    
    public android.view.View.OnClickListener myOnClickListener
                   = new android.view.View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                int id = v.getId();
                int position = id-VIEW_INDEX;
                if(position <0 || position > mKeys.length)
                    return;
                final String key = mKeys[position];
                final String title = mTitles[position];
                final String val = mVals[position];
                
                if("website".equals(key)) {
                    callBrowser(val);
                } else if("email".equals(key)) {
                    callEmail(val);
                }
            } catch (Exception e) {
                // Do nothing!
            }
        }        
    };
    
}

相關的layout檔案部分如下:
            <TextView android:id="@+id/detail_developer"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/lbl_title_developer"
                    android:textStyle="bold"
                    android:background="#008F00"
                    >
            </TextView>
            <com.xxx.yyy.LinearLayoutForListView android:id="@+id/detail_lst_web_email"
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:visibility="visible"
                android:clickable="true"
                >
            </com.xxx.yyy.LinearLayoutForListView>

以下代碼向這個假的ListView填入資料:
        com.xxx.yyy.LinearLayoutForListView lst_developer
               = (com.xxx.yyy.LinearLayoutForListView) findViewById(R.id.detail_lst_web_email);
        String keys[]={ "website", "email"};
        String titles[]={getResources().getString(R.string.lbl_title_developer_website),
                         getResources().getString(R.string.lbl_title_developer_email)};
        String vals[]={"http://abc.com", "[email protected]"};
        DeveloperInfoAdapter developerAdapter = new DeveloperInfoAdapter(this, keys, titles, vals);
        lst_developer.setOnclickLinstener(developerAdapter.myOnClickListener);
        lst_developer.setAdapter(developerAdapter);

<完>

結伴旅遊,一個免費的交友網站:www.jieberu.com

推推族,免費得門票,遊景區:www.tuituizu.com

 

[轉載]Android: 如何?ScrollView中含有ListView?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.