您好,登錄后才能下訂單哦!
本文實例為大家分享了Android仿京東左側分類條目效果的具體代碼,供大家參考,具體內容如下
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.LinearLayout; import android.widget.ScrollView; import android.widget.TextView; import com.frame.R; import java.util.ArrayList; import java.util.List; /** * Created by syd on 2016/10/9. */ public class TestActivity extends Activity { ScrollView sv_test; LinearLayout ll_test_contain; List<String> goodsList = new ArrayList<String>(); List<TextView> textViewList = new ArrayList<>(); List<View> viewList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_activity_test); initViews(); initData(); } /** * 初始化控件 */ private void initViews() { sv_test = (ScrollView) findViewById(R.id.sv_test); ll_test_contain = (LinearLayout) findViewById(R.id.ll_test_contain); } protected void initData() { goodsList.add("常用分類"); goodsList.add("潮流女裝"); goodsList.add("品牌男裝"); goodsList.add("內衣配飾"); goodsList.add("家用電器"); goodsList.add("手機數碼"); goodsList.add("電腦辦公"); goodsList.add("個護化妝"); goodsList.add("母嬰頻道"); goodsList.add("食物生鮮"); goodsList.add("酒水飲料"); goodsList.add("家居家紡"); goodsList.add("酒水飲料"); goodsList.add("整車車品"); goodsList.add("運動戶外"); goodsList.add("圖書"); goodsList.add("鐘表"); goodsList.add("居家生活"); goodsList.add("珠寶飾品"); goodsList.add("音像制品"); goodsList.add("家具建材"); goodsList.add("計生情趣"); goodsList.add("營養保健"); goodsList.add("奢侈禮品"); goodsList.add("生活服務"); goodsList.add("旅游出行"); //動態生成每一個條目 for (int i = 0; i <goodsList.size() ; i++) { View view = getLayoutInflater().inflate(R.layout.test_item_scrollview,null); view.setOnClickListener(textOnClickListener); TextView tv_item_scroll = (TextView) view.findViewById(R.id.tv_item_scroll); view.setId(i); tv_item_scroll.setText(goodsList.get(i)); ll_test_contain.addView(view); viewList.add(view); textViewList.add(tv_item_scroll); } changeTextColor(0); } private View.OnClickListener textOnClickListener = new View.OnClickListener() { @Override public void onClick(View v) { changeTextColor(v.getId()); changeTextLocation(v.getId()); } }; //改變點擊條目的位置,居中 private void changeTextLocation(int textPosition) { int x = (viewList.get(textPosition).getTop() - sv_test.getHeight()/2 + (viewList.get(textPosition).getHeight() / 2)); sv_test.smoothScrollTo(0, x); } //改變點擊條目的顏色 private void changeTextColor(int textPosition) { for (int i = 0; i <textViewList.size() ; i++) { textViewList.get(i).setTextColor(0xFF000000); textViewList.get(i).setBackgroundColor(0x00000000); } textViewList.get(textPosition).setTextColor(0xFFFF0000); textViewList.get(textPosition).setBackgroundColor(0xFFFFFFFF); } }
代碼2:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <com.frame.activity.TestScrollView android:id="@+id/sv_test" android:layout_width="80dp" android:layout_height="wrap_content"> <LinearLayout android:id="@+id/ll_test_contain" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> </LinearLayout> </com.frame.activity.TestScrollView> </LinearLayout>
代碼3:
<?xml version="1.0" encoding="utf-8"?> <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:gravity="center" android:paddingBottom="16dp" android:paddingTop="16dp" android:id="@+id/tv_item_scroll" android:layout_width="match_parent" android:layout_height="wrap_content" /> <ImageView android:background="#cccccc" android:layout_width="match_parent" android:layout_height="1dp" /> </LinearLayout>
效果圖:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。