当前位置:聪少自媒体网 > 今日头条 > 正文

Android 仿今日头条简单的刷新效果实例代码

2020-10-05 今日头条 聪少自媒体

package com.example.fragmentmytest;

import android.content.DialogInterface;

import android.graphics.Color;

import android.os.Bundle;

import android.support.v4.app.FragmentActivity;

import android.support.v4.app.FragmentManager;

import android.support.v4.app.FragmentTransaction;

import android.text.TextUtils;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.TextView;

import com.example.dialog.CustomDialog;

import com.example.dialog.CustomDialogChangePwd;

import com.example.dialog.CustomDialogSignUp;

import com.example.myapplication.CustomApplication;

import com.example.utils.ToastUtils;

public class MainActivity extends FragmentActivity {

public static final String serverAddress="http://192.168.1.101";

public static final String serverPort="8080";

OneFragment onefragment;

TwoFragment twofragment;

ThreeFragment threefragment;

FourFragment fourfragment;

Button btn1, btn2, btn3, btn4;

OnClickListener clicklistener;

TextView stu_msg;

private CustomApplication app;

FragmentManager fragementManager;

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

app=(CustomApplication) getApplication(); // 获得CustomApplication对象

// 必须继承FragmentActivity才能用getSupportFragmentManager();最好使用v4.app,已经没怎么有人使用app中的了

fragementManager=getSupportFragmentManager();

init();

// 第一次启动时选中第0个tab

setTabSelection(0);// 不能左右滑动的默认值

}

public void init() {

stu_msg=(TextView) findViewById(R.id.stu_msg);

btn1=(Button) findViewById(R.id.btn1);

btn2=(Button) findViewById(R.id.btn2);

btn3=(Button) findViewById(R.id.btn3);

btn4=(Button) findViewById(R.id.btn4);

clicklistener=new OnClickListener() {

public void onClick(View arg0) {

int id=arg0.getId();

switch (id) {

case R.id.btn1:

setTabSelection(0);

break;

case R.id.btn2:

setTabSelection(1);

break;

case R.id.btn3:

setTabSelection(2);

break;

case R.id.btn4:

setTabSelection(3);

break;

default:

break;

}

}

};

btn1.setOnClickListener(clicklistener);

btn2.setOnClickListener(clicklistener);

btn3.setOnClickListener(clicklistener);

btn4.setOnClickListener(clicklistener);

}

private void setTabSelection(int index) {

clearSelection();// 每次选中之前先清楚掉上次的选中状态

// 开启一个Fragment事务

FragmentTransaction transaction=fragementManager.beginTransaction();

// 先隐藏掉所有的Fragment,以防止有多个Fragment显示在界面上的情况

hideFragements(transaction);

switch (index) {

case 0:

btn1.setBackgroundColor(Color.parseColor("#CFEFEF"));

btn1.setTextColor(Color.parseColor("#FFFFFF"));

app.setRoom(btn1.getText().toString());

if (onefragment==null) {

onefragment=new OneFragment();

transaction.add(R.id.framelayout, onefragment);

} else {

transaction.show(onefragment);

onefragment.mPullRefreshListView.setRefreshing(true);

}

break;

case 1:

btn2.setBackgroundColor(Color.parseColor("#CFEFEF"));

btn2.setTextColor(Color.parseColor("#FFFFFF"));

app.setRoom(btn2.getText().toString());

if (twofragment==null) {

twofragment=new TwoFragment();

transaction.add(R.id.framelayout, twofragment);

} else {

transaction.show(twofragment);

twofragment.mPullRefreshListView.setRefreshing(true);

}

break;

case 2:

btn3.setBackgroundColor(Color.parseColor("#CFEFEF"));

btn3.setTextColor(Color.parseColor("#FFFFFF"));

app.setRoom(btn3.getText().toString());

if (threefragment==null) {

threefragment=new ThreeFragment();

transaction.add(R.id.framelayout, threefragment);

} else {

transaction.show(threefragment);

threefragment.mPullRefreshListView.setRefreshing(true);

}

break;

case 3:

btn4.setBackgroundColor(Color.parseColor("#CFEFEF"));

btn4.setTextColor(Color.parseColor("#FFFFFF"));

app.setRoom(btn4.getText().toString());

if (fourfragment==null) {

fourfragment=new FourFragment();

transaction.add(R.id.framelayout, fourfragment);

} else {

transaction.show(fourfragment);

fourfragment.mPullRefreshListView.setRefreshing(true);

}

break;

default:

break;

}

transaction.commit();

}

private void clearSelection() {

btn1.setBackgroundColor(Color.parseColor("#EFEFEF"));

btn1.setTextColor(Color.parseColor("#234567"));

btn2.setBackgroundColor(Color.parseColor("#EFEFEF"));

btn2.setTextColor(Color.parseColor("#234567"));

btn3.setBackgroundColor(Color.parseColor("#EFEFEF"));

btn3.setTextColor(Color.parseColor("#234567"));

btn4.setBackgroundColor(Color.parseColor("#EFEFEF"));

btn4.setTextColor(Color.parseColor("#234567"));

}

private void hideFragements(FragmentTransaction transaction) {

if (onefragment !=null) {

transaction.hide(onefragment);

}

if (twofragment !=null) {

transaction.hide(twofragment);

}

if (threefragment !=null) {

transaction.hide(threefragment);

}

if (fourfragment !=null) {

transaction.hide(fourfragment);

}

}

}

聪少爱学堂聪少
聪少爱学堂创始人,梅州市鹏鑫网络科技有限公司CEO,09年开始踏入互联网,10年互联网行业经验,资深自媒体人,自媒体优秀导师,咪挺微商团对营销引流顾问,业务包含:精准引流技术/代引流精准粉,专业小红书,知乎,微博代运营。
  • 38988文章总数
  • 1491135访问次数
  • 建站天数
  • 合作伙伴