Android使用Scroll+Fragment仿京東分類效果
本文實(shí)例為大家分享了Android九宮格圖片展示的具體代碼,供大家參考,具體內(nèi)容如下

實(shí)現(xiàn)思路:首先說下布局,整個是一個橫向的線性布局,左邊是一個ScrollView,右邊是一個FrameLayout,在代碼中動態(tài)向ScrollView中添加TextView,然后根據(jù)TextView的點(diǎn)擊事件使用Fragment替換FrameLayout
首先看下布局:
<LinearLayout 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"
android:orientation="horizontal"
tools:context="www.jdsort.com.jdsort.MainActivity">
<ScrollView
android:id="@+id/scrollview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scrollbars="none">
<LinearLayout
android:id="@+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical" />
</ScrollView>
<FrameLayout
android:id="@+id/framelayout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"/>
</LinearLayout>
MainActivity代碼:
public class MainActivity extends FragmentActivity implements View.OnClickListener {
private String[] titles={"常用分類","潮流女裝","品牌男裝","內(nèi)衣配飾","家用電器","手機(jī)數(shù)碼","電腦辦公","個護(hù)化妝","母嬰頻道","食物生鮮","酒水飲料","家居家紡","整車車品","鞋靴箱包","運(yùn)動戶外","圖書","玩具樂器","鐘表","居家生活","珠寶飾品","音像制品","家具建材","計(jì)生情趣","營養(yǎng)保健","奢侈禮品","生活服務(wù)","旅游出行"};
private ScrollView mScrollView;
private FrameLayout mFrameLayout;
//裝裝ScrollView的item的TextView的數(shù)組
private TextView[] textViewArray;
//裝ScrollView的item的數(shù)組
private View[] views;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
textViewArray=new TextView[titles.length];
views=new View[titles.length];
initView();
getSupportFragmentManager().beginTransaction().replace(R.id.framelayout,new FragmentOne()).commit();
}
private void initView() {
mScrollView= (ScrollView) findViewById(R.id.scrollview);
addView();
changeTextColor(0);
mFrameLayout= (FrameLayout) findViewById(R.id.framelayout);
}
/**
* 給ScrollView添加子View
*/
private void addView() {
LinearLayout mLinearLayout= (LinearLayout) findViewById(R.id.linearlayout);
View view;
for(int x=0;x<titles.length;x++){
view = View.inflate(this, R.layout.item_scrollview, null);
view.setId(x);
view.setOnClickListener(this);
TextView tv= (TextView) view.findViewById(R.id.textview);
tv.setText(titles[x]);
mLinearLayout.addView(view);
textViewArray[x]=tv;
views[x]=view;
}
}
@Override
public void onClick(View v) {
ToastUtils.showToast(this,titles[(int) v.getId()]);
changeTextColor((int) v.getId());
changeTextLocation((int) v.getId());
Fragment fragment=null;
switch (v.getId()){
case 0:
fragment=new FragmentOne();
break;
case 1:
fragment=new Fragment1();
break;
case 2:
fragment=new Fragment2();
break;
case 3:
fragment=new Fragment3();
break;
case 4:
fragment=new Fragment4();
break;
case 5:
fragment=new Fragment5();
break;
case 6:
fragment=new Fragment6();
break;
case 7:
fragment=new Fragment7();
break;
}
if(fragment!=null){
getSupportFragmentManager().beginTransaction().replace(R.id.framelayout,fragment).commit();
}
}
/**
* 改變textView的顏色
* @param id
*/
private void changeTextColor(int id) {
for (int i = 0; i < textViewArray.length; i++) {
if(i!=id){
textViewArray[i].setBackgroundResource(android.R.color.transparent);
textViewArray[i].setTextColor(0xff000000);
}else {
textViewArray[id].setBackgroundResource(android.R.color.white);
textViewArray[id].setTextColor(0xffff5d5e);
}
}
}
/**
* 改變欄目位置
*/
private void changeTextLocation(int index) {
//views[clickPosition].getTop()針對其父視圖的頂部相對位置
int x = (views[index].getTop() - mScrollView.getHeight() / 2);
mScrollView.smoothScrollTo(0, x);
}
}
源碼等上傳上去會再次發(fā)布鏈接,如果那里寫的不好,歡迎私信,評論指導(dǎo)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android開發(fā)學(xué)習(xí)筆記之通過API接口將LaTex數(shù)學(xué)函數(shù)表達(dá)式轉(zhuǎn)化為圖片形式
這篇文章主要介紹了Android開發(fā)學(xué)習(xí)筆記之通過API接口將LaTex數(shù)學(xué)函數(shù)表達(dá)式轉(zhuǎn)化為圖片形式的相關(guān)資料,需要的朋友可以參考下2015-11-11
Android 靜默方式實(shí)現(xiàn)批量安裝卸載應(yīng)用程序的深入分析
本篇文章是對Android 靜默方式實(shí)現(xiàn)批量安裝卸載應(yīng)用程序進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
Android 動態(tài)添加view或item并獲取數(shù)據(jù)的實(shí)例
下面小編就為大家?guī)硪黄狝ndroid 動態(tài)添加view或item并獲取數(shù)據(jù)的實(shí)例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
Android?Jetpack結(jié)構(gòu)運(yùn)用Compose實(shí)現(xiàn)微博長按點(diǎn)贊彩虹效果
Compose在動畫方面下足了功夫,提供了豐富的API。但也正由于API種類繁多,如果想一氣兒學(xué)下來,最終可能會消化不良,導(dǎo)致似懂非懂。結(jié)合例子學(xué)習(xí)是一個不錯的方法,本文就帶大家邊學(xué)邊做,通過實(shí)現(xiàn)一個微博長按點(diǎn)贊的動畫效果,學(xué)習(xí)了解Compose動畫的常見思路和開發(fā)技巧2022-07-07
Flutter實(shí)現(xiàn)自定義搜索框AppBar的示例代碼
開發(fā)中,頁面頭部為搜索樣式的設(shè)計(jì)非常常見,為了可以像系統(tǒng)AppBar那樣使用,本文將利用Flutter自定義一個搜索框,感興趣的可以了解一下2022-04-04
android Tween Animation屬性設(shè)置方法實(shí)例
在Android開發(fā)中,Animation是用來給控件制作效果的,本文介紹二種實(shí)現(xiàn)方法2013-11-11
Android優(yōu)雅地處理按鈕重復(fù)點(diǎn)擊的幾種方法
這篇文章主要介紹了Android優(yōu)雅地處理按鈕重復(fù)點(diǎn)擊的幾種方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09
深入學(xué)習(xí)Android?ANR?的原理分析及解決辦法
Android系統(tǒng)中,AMS和WMS會檢測App的響應(yīng)時間,如果App在特定時間無法相應(yīng)屏幕觸摸或鍵盤輸入時間,或者特定事件沒有處理完畢,就會出現(xiàn)ANR。本文將帶領(lǐng)大學(xué)深入學(xué)習(xí)一下ANR的原理及解決辦法,感興趣的同學(xué)可以學(xué)習(xí)一下2021-11-11

