首先先写一下主Activity的代码吧
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView image_btn;
private TextView text_daizhi;
private TextView text_yizhi;
private TextView text_yiqu;
private FrameLayout frame_layout;
private View item_popup;
private View view;
private PopupWindow popupWindow;
private TextView btn_daizhi;
private TextView btn_yizhi;
private TextView btn_yiqu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image_btn = findViewById(R.id.image_btn);
text_daizhi = findViewById(R.id.text_daizhi);
text_yizhi = findViewById(R.id.text_yizhi);
text_yiqu = findViewById(R.id.text_yiqu);
frame_layout = findViewById(R.id.frame_layout);
text_daizhi.setOnClickListener(this);
text_yizhi.setOnClickListener(this);
text_yiqu.setOnClickListener(this);
Frag_Daizhi frag_daizhi = new Frag_Daizhi();
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_layout,frag_daizhi).commit();
item_popup = View.inflate(this, R.layout.item_popup, null);
view = View.inflate(this, R.layout.activity_main, null);
popupWindow = new PopupWindow(item_popup
, ActionBar.LayoutParams.WRAP_CONTENT,ActionBar.LayoutParams.WRAP_CONTENT);
popupWindow.setTouchable(true);
btn_daizhi = item_popup.findViewById(R.id.btn_daizhi);
btn_yizhi = item_popup.findViewById(R.id.btn_yizhi);
btn_yiqu = item_popup.findViewById(R.id.btn_yiqu);
btn_daizhi.setOnClickListener(this);
btn_yizhi.setOnClickListener(this);
btn_yiqu.setOnClickListener(this);
image_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//popupWindow.showAtLocation(view, Gravity.BOTTOM,0,0);
popupWindow.showAsDropDown(image_btn);
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.text_daizhi:
Frag_Daizhi frag_daizhi = new Frag_Daizhi();
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_layout,frag_daizhi).commit();
break;
case R.id.text_yizhi:
Frag_YiZhi frag_yiZhi = new Frag_YiZhi();
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_layout,frag_yiZhi).commit();
break;
case R.id.text_yiqu:
Frag_YiQu frag_yiQu = new Frag_YiQu();
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_layout,frag_yiQu).commit();
break;
case R.id.btn_daizhi:
Frag_Daizhi frag_daizhi2 = new Frag_Daizhi();
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_layout,frag_daizhi2).commit();
popupWindow.dismiss();
break;
case R.id.btn_yizhi:
Frag_YiZhi frag_yiZhi2 = new Frag_YiZhi();
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_layout,frag_yiZhi2).commit();
popupWindow.dismiss();
break;
case R.id.btn_yiqu:
Frag_YiQu frag_yiQu2 = new Frag_YiQu();
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_layout,frag_yiQu2).commit();
popupWindow.dismiss();
break;
}
}
}
main的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:background="#fff"
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="20sp"
android:text="订单列表"/>
<ImageView
android:id="@+id/image_btn"
android:layout_width="25dp"
android:layout_height="25dp"
android:clickable="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"
android:background="@drawable/lv_icon"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:id="@+id/text_daizhi"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:layout_height="match_parent"
android:textSize="16sp"
android:background="@drawable/biankuang"
android:text="待支付"/>
<TextView
android:id="@+id/text_yizhi"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:layout_height="match_parent"
android:textSize="16sp"
android:background="@drawable/biankuang"
android:text="已支付"/>
<TextView
android:id="@+id/text_yiqu"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:layout_height="match_parent"
android:textSize="16sp"
android:background="@drawable/biankuang"
android:text="已取消"/>
</LinearLayout>
<FrameLayout
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
</LinearLayout>
这些代码是切换popupWindow 以及Fragment的
要想实现切换首先要有popupWindow的那些选项 item_popup
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="100dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/btn_daizhi"
android:layout_width="match_parent"
android:layout_weight="1"
android:gravity="center"
android:layout_height="0dp"
android:textSize="16sp"
android:padding="15dp"
android:background="@drawable/select_dianji"
android:text="待支付"/>
<TextView
android:id="@+id/btn_yizhi"
android:layout_width="match_parent"
android:layout_weight="1"
android:gravity="center"
android:layout_height="0dp"
android:textSize="16sp"
android:padding="15dp"
android:background="@drawable/select_dianji"
android:text="已支付"/>
<TextView
android:id="@+id/btn_yiqu"
android:layout_width="match_parent"
android:layout_weight="1"
android:gravity="center"
android:layout_height="0dp"
android:textSize="16sp"
android:padding="15dp"
android:background="@drawable/select_dianji"
android:text="已取消"/>
</LinearLayout>
</RelativeLayout>
接下来就要写Fragment啦
这是待支付的Fragment的代码
public class Frag_Daizhi extends Fragment implements Maindingdan {
private List<MyDataDingDanBean.DataBean> list = new ArrayList<>();
private View view;
private RecyclerView recycler_daizhi;
private Presenterdingdanq presenter;
private RelativeLayout relative_bar;
private SmartRefreshLayout smart_refresh;
private int page=0;
private MyDingDanAdapter adapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.frag_daizhi, container, false);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
recycler_daizhi = view.findViewById(R.id.recycler_daizhi);
relative_bar = view.findViewById(R.id.relative_bar);
smart_refresh = view.findViewById(R.id.smart_refresh);
presenter = new Presenterdingdanq(this);
}
@Override
public void onResume() {
super.onResume();
relative_bar.setVisibility(View.VISIBLE);
presenter.getDingUrl(ApiUtil.dingdan,page);
}
@Override
public void getDingDanBean(final MyDataDingDanBean myDingDanBean) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (myDingDanBean!=null){
list.addAll(myDingDanBean.getData());
myAdapter();
relative_bar.setVisibility(View.GONE);
smart_refresh.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(RefreshLayout refreshlayout) {
list.clear();
list.addAll(0,myDingDanBean.getData());
myAdapter();
smart_refresh.finishRefresh();
}
});
smart_refresh.setOnLoadmoreListener(new OnLoadmoreListener() {
@Override
public void onLoadmore(RefreshLayout refreshlayout) {
list.clear();
page++;
presenter.getDingUrl(ApiUtil.dingdan,page);
onResume();
list.addAll(myDingDanBean.getData());
myAdapter();
smart_refresh.finishLoadmore();
}
});
}
}
});
}
public void myAdapter(){
if (adapter==null){
recycler_daizhi.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));
adapter = new MyDingDanAdapter(getActivity(), list,presenter,page);
recycler_daizhi.setAdapter(adapter);
}else {
adapter.notifyDataSetChanged();
}
}
}
这是Fragment的布局
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/smart_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_daizhi"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</ScrollView>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
<RelativeLayout
android:id="@+id/relative_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</RelativeLayout>
接下来就要写适配器了
public class MyDingDanAdapter extends RecyclerView.Adapter<MyDingDanHolder> {
private final Context context;
private final List<MyDataDingDanBean.DataBean> list;
private Presenterdingdanq presenter;
private int page;
public MyDingDanAdapter(Context context, List<MyDataDingDanBean.DataBean> list, Presenterdingdanq presenter, int page) {
this.context = context;
this.list = list;
this.presenter = presenter;
this.page = page;
}
@Override
public MyDingDanHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_dingdan,parent, false);
MyDingDanHolder holder = new MyDingDanHolder(view);
return holder;
}
@Override
public void onBindViewHolder(MyDingDanHolder holder, final int position) {
holder.text_title.setText(list.get(position).getTitle());
holder.text_price.setText("价格: "+list.get(position).getPrice());
holder.text_tame.setText(list.get(position).getCreatetime());
if (list.get(position).getStatus()==0){
holder.text_daizhifu.setText("待支付");
holder.text_daizhifu.setTextColor(Color.RED);
holder.text_btn.setText("取消订单");
holder.text_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("提示");
builder.setMessage("确定要取消订单吗?");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Map<String, String> params=new HashMap<>();
params.put("uid","3690");
params.put("orderId", String.valueOf(list.get(position).getOrderid()));
params.put("status", String.valueOf(2));
OkHttp3Util.doPost(ApiUtil.genxin, params, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()){
Log.d("+++++++++响哥1", String.valueOf(list.get(position).getStatus()));
presenter.getDingUrl(ApiUtil.dingdan,page);
}
}
});
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
});
}else if (list.get(position).getStatus()==1){
holder.text_daizhifu.setText("已支付");
holder.text_daizhifu.setTextColor(Color.BLACK);
holder.text_btn.setText("查看订单");
}else if (list.get(position).getStatus()==2){
holder.text_daizhifu.setText("已取消");
holder.text_daizhifu.setTextColor(Color.BLACK);
holder.text_btn.setText("查看订单");
holder.text_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("提示");
builder.setMessage("确定循环利用吗?");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Map<String, String> params = new HashMap<>();
params.put("uid", "3690");
params.put("orderId", String.valueOf(list.get(position).getOrderid()));
params.put("status", String.valueOf(0));
OkHttp3Util.doPost(ApiUtil.genxin, params, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
Log.d("+++++++++响哥", String.valueOf(list.get(position).getStatus()));
presenter.getDingUrl(ApiUtil.dingdan, page);
}
}
});
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
});
}
}
@Override
public int getItemCount() {
return list.size();
}
}
适配器的布局
<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:orientation="horizontal">
<TextView
android:id="@+id/text_title"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="bottom"
android:layout_height="match_parent"
android:text="1111"/>
<TextView
android:id="@+id/text_daizhifu"
android:layout_width="50dp"
android:gravity="bottom"
android:layout_height="match_parent"
android:textColor="#ff0000"
android:text="待支付"/>
</LinearLayout>
<TextView
android:id="@+id/text_price"
android:gravity="bottom"
android:textColor="#ff0000"
android:layout_marginLeft="20dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="1111"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:orientation="horizontal">
<TextView
android:id="@+id/text_tame"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="bottom"
android:layout_height="match_parent"
android:text="1111"/>
<TextView
android:id="@+id/text_btn"
android:layout_width="70dp"
android:gravity="center"
android:layout_gravity="bottom"
android:layout_height="30dp"
android:background="@drawable/biankuang"
android:text="取消订单"/>
</LinearLayout>
<TextView
android:layout_marginTop="15dp"
android:clickable="true"
android:focusable="false"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#9999"/>
</LinearLayout>
//边框线
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="0.2dp" android:color="#000"/>
<solid android:color="#fff"/>
</shape>