【移動應(yīng)用開發(fā)技術(shù)】怎么在Android中實(shí)現(xiàn)一個(gè)購物分類效果_第1頁
【移動應(yīng)用開發(fā)技術(shù)】怎么在Android中實(shí)現(xiàn)一個(gè)購物分類效果_第2頁
【移動應(yīng)用開發(fā)技術(shù)】怎么在Android中實(shí)現(xiàn)一個(gè)購物分類效果_第3頁
【移動應(yīng)用開發(fā)技術(shù)】怎么在Android中實(shí)現(xiàn)一個(gè)購物分類效果_第4頁
【移動應(yīng)用開發(fā)技術(shù)】怎么在Android中實(shí)現(xiàn)一個(gè)購物分類效果_第5頁
已閱讀5頁,還剩15頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

【移動應(yīng)用開發(fā)技術(shù)】怎么在Android中實(shí)現(xiàn)一個(gè)購物分類效果

怎么在Android中實(shí)現(xiàn)一個(gè)購物分類效果?相信很多沒有經(jīng)驗(yàn)的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。Android是一種基于Linux內(nèi)核的自由及開放源代碼的操作系統(tǒng),主要使用于移動設(shè)備,如智能手機(jī)和平板電腦,由美國Google公司和開放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。SecondActivity.javapublic

class

SecondActivity

extends

AppCompatActivity{

private

ViewPager

mPager;

private

RadioGroup

mRadioGroup;

@Override

protected

void

onCreate(@Nullable

Bundle

savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.second);

mPager

=

findViewById(R.id.pager);

mRadioGroup

=

findViewById(R.id.group);

mPager.setAdapter(new

FragmentPagerAdapter(getSupportFragmentManager())

{

@Override

public

Fragment

getItem(int

position)

{

Fragment

fragment=null;

switch

(position){

case

0:

fragment

=

new

Afrag();

break;

case

1:

fragment

=

new

Bfrag();

break;

case

2:

fragment

=

new

Cfrag();

break;

}

return

fragment;

}

@Override

public

int

getCount()

{

return

3;

}

});

mRadioGroup.setOnCheckedChangeListener(new

RadioGroup.OnCheckedChangeListener()

{

@Override

public

void

onCheckedChanged(RadioGroup

radioGroup,

int

i)

{

switch

(i){

case

R.id.r1:

mPager.setCurrentItem(0);

break;

case

R.id.r2:

mPager.setCurrentItem(1);

break;

case

R.id.r3:

mPager.setCurrentItem(2);

break;

}

}

});

}

}secondActivity.xml<LinearLayout

xmlns:android="/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

<android.support.v4.view.ViewPager

android:id="@+id/pager"

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="13"/>

<RadioGroup

android:id="@+id/group"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:orientation="horizontal">

<RadioButton

android:id="@+id/r1"

android:layout_width="wrap_content"

android:layout_weight="1"

android:layout_height="match_parent"

android:button="@null"

android:gravity="center"

android:text="首頁"/>

<RadioButton

android:id="@+id/r2"

android:layout_width="wrap_content"

android:layout_weight="1"

android:layout_height="match_parent"

android:button="@null"

android:gravity="center"

android:text="分類"/>

<RadioButton

android:id="@+id/r3"

android:layout_width="wrap_content"

android:layout_weight="1"

android:layout_height="match_parent"

android:button="@null"

android:gravity="center"

android:text="購物車"/>

</RadioGroup>

</LinearLayout>Afrag.javapublic

class

Afrag

extends

Fragment{

private

RecyclerView

mRecyclerView;

public

String

urlString="/tools/mockapi/6523/restaurants_offset_0_limit_4

";

@Nullable

@Override

public

View

onCreateView(@NonNull

LayoutInflater

inflater,

@Nullable

ViewGroup

container,

@Nullable

Bundle

savedInstanceState)

{

View

view

=

View.inflate(getActivity(),

R.layout.afrag,

null);

mRecyclerView

=

view.findViewById(R.id.rv);

initData();

return

view;

}

private

void

initData()

{

OkHttps

okHttps

=

OkHttps.getInstance();

okHttps.doGet(urlString,

new

OkHttps.OkCallback()

{

@Override

public

void

onResponse(String

json)

{

//瀑布流

PubuBean

pubuBean

=

new

Gson().fromJson(json,PubuBean.class);

List<PubuBean.DataBean>

results

=

pubuBean.getData();

PubuAdapter

pubuAdapter

=

new

PubuAdapter(getActivity());

pubuAdapter.setDatas(results);

mRecyclerView.setAdapter(pubuAdapter);

StaggeredGridLayoutManager

manager

=

new

StaggeredGridLayoutManager(2,

StaggeredGridLayoutManager.VERTICAL);

mRecyclerView.setLayoutManager(manager);

}

@Override

public

void

onFailure(Exception

e)

{

}

});

}

}afrag.xml<LinearLayout

xmlns:android="/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView

android:id="@+id/rv"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

</LinearLayout>PubuAdapter.javapublic

class

PubuAdapter

extends

RecyclerView.Adapter<PubuAdapter.ViewHolder>{

private

Context

mContext;

ArrayList<PubuBean.DataBean>

list;

public

PubuAdapter(Context

context)

{

mContext

=

context;

list

=

new

ArrayList<>();

}

public

void

setDatas(List<PubuBean.DataBean>

datas)

{

list.addAll(datas);

}

@NonNull

@Override

public

ViewHolder

onCreateViewHolder(@NonNull

ViewGroup

parent,

int

viewType)

{

View

view

=

View.inflate(mContext,

R.layout.pbitem,

null);

ViewHolder

holder

=

new

ViewHolder(view);

return

holder;

}

@Override

public

void

onBindViewHolder(@NonNull

ViewHolder

holder,

int

position)

{

holder.mTextView.setText(list.get(position).getName());

Glide.with(mContext).load(list.get(position).getPic_url()).into(holder.mImageView);

}

@Override

public

int

getItemCount()

{

return

list.size();

}

public

class

ViewHolder

extends

RecyclerView.ViewHolder

{

ImageView

mImageView;

TextView

mTextView;

public

ViewHolder(View

itemView)

{

super(itemView);

mImageView

=

itemView.findViewById(R.id.image);

mTextView

=

itemView.findViewById(R.id.t1);

}

}

}Bfrag.javapublic

class

Bfrag

extends

Fragment{

private

RecyclerView

mRecyclerView;

LeftAdapter

adapter;

String

urlString="/product/getCatagory";

String

urlString2="/product/getProductCatagory";

private

RecyclerView

mRecyclerView2;

@Nullable

@Override

public

View

onCreateView(@NonNull

LayoutInflater

inflater,

@Nullable

ViewGroup

container,

@Nullable

Bundle

savedInstanceState)

{

View

view

=

View.inflate(getActivity(),

R.layout.bfrag,

null);//分類頁面

mRecyclerView

=

view.findViewById(R.id.rv1);

//左側(cè)列表

mRecyclerView2

=

view.findViewById(R.id.rv2);

//右

initData();

return

view;

}

private

void

initData()

{

OkHttps

okHttps

=

OkHttps.getInstance();

okHttps.doGet(urlString,

new

OkHttps.OkCallback()

{

@Override

public

void

onResponse(String

json)

{

//瀑布流

LeftBean

leftBean

=

new

Gson().fromJson(json,LeftBean.class);

List<LeftBean.DataBean>

results

=

leftBean.getData();

adapter

=

new

LeftAdapter(getActivity());

adapter.setDatas(results);

mRecyclerView.setAdapter(adapter);

mRecyclerView.setLayoutManager(new

LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));

adapter.setOnItemClickListener(new

LeftAdapter.OnItemClickListener()

{

@Override

public

void

onItemClick(int

position)

{

//Toast.makeText(getActivity(),position+"",Toast.LENGTH_SHORT).show();

rigth(position);

}

});

}

@Override

public

void

onFailure(Exception

e)

{

}

});

}

public

void

rigth(int

position){

HashMap<String,

String>

map

=

new

HashMap<>();

map.put("cid",position+1+"");

OkHttps.getInstance().doPost(urlString2,map,new

OkHttps.OkCallback()

{

@Override

public

void

onResponse(String

json)

{

RightBean

rightBean

=

new

Gson().fromJson(json,RightBean.class);

List<RightBean.DataBean>

results

=

rightBean.getData();

RightAdapter

rightAdapter

=

new

RightAdapter(getActivity());

rightAdapter.setDatas(results);

mRecyclerView2.setAdapter(rightAdapter);

mRecyclerView2.setLayoutManager(new

GridLayoutManager(getActivity(),3));

}

@Override

public

void

onFailure(Exception

e)

{

}

});

}

}bfrag.xml<android.support.v7.widget.RecyclerView

android:id="@+id/rv1"

android:layout_width="0dp"

android:layout_weight="2"

android:layout_height="match_parent"/>

<android.support.v7.widget.RecyclerView

android:id="@+id/rv2"

android:layout_width="0dp"

android:layout_weight="10"

android:layout_height="match_parent"/>

</LinearLayout>LeftAdapter.javapublic

class

LeftAdapter

extends

RecyclerView.Adapter<LeftAdapter.ViewHolder>{

private

Context

mContext;

ArrayList<LeftBean.DataBean>

list;

public

LeftAdapter(Context

context)

{

mContext

=

context;

list

=

new

ArrayList<>();

}

public

void

setDatas(List<LeftBean.DataBean>

datas)

{

list.addAll(datas);

}

public

interface

OnItemClickListener{

//自定義接口回調(diào)設(shè)置點(diǎn)擊事件

void

onItemClick(int

position);

}

private

OnItemClickListener

mOnItemClickListener;

public

void

setOnItemClickListener(OnItemClickListener

onItemClickListener){

mOnItemClickListener=onItemClickListener;

}

@NonNull

@Override

public

ViewHolder

onCreateViewHolder(@NonNull

ViewGroup

parent,

int

viewType)

{

View

view

=

View.inflate(mContext,

R.layout.leftitem,

null);

ViewHolder

holder

=

new

ViewHolder(view);

return

holder;

}

@Override

public

void

onBindViewHolder(@NonNull

final

ViewHolder

holder,

int

position)

{

holder.mTextView.setText(list.get(position).getName());

holder.itemView.setOnClickListener(new

View.OnClickListener()

{

@Override

public

void

onClick(View

view)

{

int

position1

=

holder.getLayoutPosition();

mOnItemClickListener.onItemClick(position1);

}

});

}

@Override

public

int

getItemCount()

{

return

list.size();

}

public

class

ViewHolder

extends

RecyclerView.ViewHolder

{

TextView

mTextView;

public

ViewHolder(View

itemView)

{

super(itemView);

mTextView

=

itemView.findViewById(R.id.text1);

}

}

}RightAdapter.javapublic

class

RightAdapter

extends

RecyclerView.Adapter<RightAdapter.ViewHolder>{

private

Context

mContext;

ArrayList<RightBean.DataBean>

list;

public

RightAdapter(Context

context)

{

mContext

=

context;

list

=

new

ArrayList<>();

}

public

void

setDatas(List<RightBean.DataBean>

datas)

{

list.addAll(datas);

}

@NonNull

@Override

public

ViewHolder

onCreateViewHolder(@NonNull

ViewGroup

parent,

int

viewType)

{

View

view

=

View.inflate(mContext,

R.layout.rightitem,

null);

ViewHolder

holder

=

new

ViewHolder(view);

return

holder;

}

@Override

public

void

onBindViewHolder(@NonNull

ViewHolder

holder,

int

position)

{

holder.mTextView.setText(list.get(position).getName());

Glide.with(mContext).load(list.get(position).getList().get(position).getIcon()).into(holder.mImageView);

}

@Override

public

int

getItemCount()

{

return

list.size();

}

public

class

ViewHolder

extends

RecyclerView.ViewHolder

{

ImageView

mImageView;

TextView

mTextView;

public

ViewHolder(View

itemView)

{

super(itemView);

mImageView

=

itemView.findViewById(R.id.image1);

mTextView

=

itemView.findViewById(R.id.text2);

}

}

}OkHttps.javapublic

class

OkHttps

{

Handler

handler;

private

final

OkHttpClient

mOkHttpClient;

private

static

OkHttps

mOkHttps;

private

OkHttps()

{

handler

=

new

Handler(Looper.getMainLooper());

LoggingInterceptor

loggingInterceptor

=

new

LoggingInterceptor();

//日志攔截器

mOkHttpClient

=

new

OkHttpClient.Builder()

.addInterceptor(loggingInterceptor)

//設(shè)置日志攔截器

.connectTimeout(5000,

TimeUnit.MILLISECONDS)

.readTimeout(5000,TimeUnit.MILLISECONDS)

.writeTimeout(5000,TimeUnit.MILLISECONDS)

.build();

}

public

static

OkHttps

getInstance(){

if

(mOkHttps==null){

synchronized

(OkHttps.class){

if

(mOkHttps==null){

return

mOkHttps=new

OkHttps();

}

}

}

return

mOkHttps;

}

public

interface

OkCallback{

void

onResponse(String

json);

void

onFailure(Exception

e);

}

public

void

doPost(String

url,

Map<String,String>

map,

final

OkCallback

okCallback){

FormBody.Builder

builder

=

new

FormBody.Builder();

if

(map!=null){

for

(String

key:map.keySet()){

builder.add(key,map.get(key));

}

}

FormBody

formBody

=

builder.build();

final

Request

request

=

new

Request.Builder()

.post(formBody)

.url(url)

.build();

Call

call

=

mOkHttpClient.newCall(request);

call.enqueue(new

Callback()

{

@Override

public

void

onFailure(Call

call,

final

IOException

e)

{

if

(okCallback!=null){

handler.post(new

Runnable()

{

@Override

public

void

run()

{

okCallback.onFailure(e);

}

});

}

}

@Override

public

void

onResponse(Call

call,

Response

response)

throws

IOException

{

try

{

if

(response!=null

&&

response.isSuccessful()){

final

String

json

=

response.body().string();

handler.post(new

Runnable()

{

@Override

public

void

run()

{

if

(okCallback!=null){

okCallback.onResponse(json);

return;

}

}

});

}

}catch

(IOException

e){

e.printStackTrace();

}

if

(okCallback!=null){

okCallback.onFailure(new

Exception("網(wǎng)絡(luò)異常"));

}

}

});

}

public

void

doGet(String

url,

final

OkCallback

okCallback){

Request

request

=

new

Request.Builder()

.url(url)

.build();

Call

call

=

mOkHttpClient.newCall(request);

call.enqueue(new

Callback()

{

@Override

public

void

onFailure(Call

call,

final

IOException

e)

{

if

(okCallback

!=

null)

{

//切換到主線程

handler.post(new

Runnable()

{

@Override

public

void

run()

{

okCallback

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論