Android仿京東分類
最近工作上也不忙,抽空學習學習,就想到了這個效果,寫出來供分享,程式碼寫的有點粗,不過能看懂,下面將一些核心程式碼貼出來供參考,掃描下方二維碼,獲取完整原始碼。
效果圖
核心程式碼
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/jzfw_top_layout"
android:layout_width="match_parent"
android:layout_height="42dp"
android:background="@drawable/home_title"
android:gravity="center_vertical" >
<TextView
android:id="@+id/jzfw_top_layout_02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="仿京東分類"
android:textColor="#ffffff"
android:textSize="20sp" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#cdcdcd" />
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#ededed"
android:gravity="center"
android:text="全部分類"
android:textColor="#333333"
android:textSize="17sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fbfbfb"
android:orientation="horizontal" >
<ListView
android:id="@+id/listview"
android:layout_width="0dp"
android:layout_height="match_parent"
android:scrollbars="none"
android:layout_weight="1.0"
android:background="#f4f4f4" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#cdcdcd" />
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3.0" />
</LinearLayout>
</LinearLayout>
listview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="60dp"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
myfragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
MainActivity.java
package com.example.listviewfragmentdemo;
import com.example.listviewfragmentdemo.adapter.MyAdapter;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class MainActivity extends FragmentActivity implements
OnItemClickListener {
private String[] strs = { "常用分類", "服飾內衣", "鞋靴", "手機", "家用電器", "數碼", "電腦辦公",
"個護化妝", "圖書" };
private ListView listView;
private MyAdapter adapter;
private MyFragment myFragment;
public static int mPosition;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉標題欄
setContentView(R.layout.activity_main);
initView();
}
/**
* 初始化view
*/
private void initView() {
listView = (ListView) findViewById(R.id.listview);
adapter = new MyAdapter(this, strs);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
//建立MyFragment物件
myFragment = new MyFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, myFragment);
//通過bundle傳值給MyFragment
Bundle bundle = new Bundle();
bundle.putString(MyFragment.TAG, strs[mPosition]);
myFragment.setArguments(bundle);
fragmentTransaction.commit();
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//拿到當前位置
mPosition = position;
//即使重新整理adapter
adapter.notifyDataSetChanged();
for (int i = 0; i < strs.length; i++) {
myFragment = new MyFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, myFragment);
Bundle bundle = new Bundle();
bundle.putString(MyFragment.TAG, strs[position]);
myFragment.setArguments(bundle);
fragmentTransaction.commit();
}
}
}
MyAdapter.java
package com.example.listviewfragmentdemo.adapter;
import com.example.listviewfragmentdemo.MainActivity;
import com.example.listviewfragmentdemo.R;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
@SuppressLint("ViewHolder")
public class MyAdapter extends BaseAdapter {
private Context context;
private String[] strings;
public static int mPosition;
public MyAdapter(Context context, String[] strings){
this.context =context;
this.strings = strings;
}
@Override
public int getCount() {
return strings.length;
}
@Override
public Object getItem(int position) {
return strings[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(context).inflate(R.layout.listview_item, null);
TextView tv = (TextView) convertView.findViewById(R.id.tv);
mPosition = position;
tv.setText(strings[position]);
if (position == MainActivity.mPosition) {
convertView.setBackgroundResource(R.drawable.tongcheng_all_bg01);
} else {
convertView.setBackgroundColor(Color.parseColor("#f4f4f4"));
}
return convertView;
}
}
MyFragment.java
package com.example.listviewfragmentdemo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MyFragment extends Fragment {
public static final String TAG = "MyFragment";
private String str;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.myfragment, null);
TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
//得到資料
str = getArguments().getString(TAG);
tv_title.setText(str);
return view;
}
}
掃描下方二維碼,獲取完整原始碼
歡迎加入QQ群和關注微信公眾號
請不要重複新增
一群:259182457 二群:471496394
三群:137038987 四群:256636915
相關文章
- 京東獲得jd商品分類API介面(父分類、根分類、子分類)API
- 高仿京東Android App,整合React-Native熱更功能AndroidAPPReact
- 仿淘寶,京東多級地址選擇器
- Android 實現GridView的橫向滾動,實現仿京東秒殺效果AndroidView
- 仿京東商城原始碼具備哪些功能?原始碼
- mui仿京東地址 css + js (動態資料)UICSSJS
- Android 遊戲引擎分類彙總Android遊戲引擎
- 仿京東商城的更多介面的實現(一)
- Android許可權處理分類Android
- 高仿京東到家APP引導頁炫酷動畫效果APP動畫
- 仿寫Android的ActivityAndroid
- Android 仿微信, QQ 裁剪Android
- 仿知乎日報androidAndroid
- Android ListView實現品種分類效果AndroidView
- android仿微信表情雨下落!Android
- Android仿打字機打字效果Android
- 仿健客、京東、天貓下拉重新整理載入動畫實現動畫
- 仿淘寶、京東拖拽商品詳情(可巢狀ViewPager、ListView、WebView、FragmentTabhost)巢狀ViewpagerWebViewFragment
- wordpress 獲取分類ID,分類標題,分類描述,分類連結url函式函式
- Android 仿知乎分享控制元件Android控制元件
- Android高仿QQ小紅點Android
- 分類 和 聚類聚類
- 仿百度文庫文件上傳頁面的多級聯動分類選擇器
- ML.NET 示例:多類分類之問題分類
- ML.NET 示例:多類分類之鳶尾花分類
- Flutter仿Android生命週期LifecycleStateFlutterAndroid
- Android自定義View之高仿QQ健康AndroidView
- Android簡單仿微博個人資訊頁Android
- SQL分類SQL
- 分類-CategoryGo
- 模式分類模式
- Nosql分類SQL
- 分類器
- 分類2
- js仿百度文庫文件上傳頁面的分類選擇器_第二版JS
- Wait Events的分類及分類依據AI
- Flutter仿京東商城專案:支援最新的Flutter 3.x及鴻蒙OSFlutter鴻蒙
- IP地址分類(A類 B類 C類 D類 E類)