页面滑动/侧滑页面
程序员文章站
2022-05-01 19:28:44
...
MainActivity
mianactivity布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#467"
android:orientation="vertical">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
android:src="@mipmap/ic_launcher"/>
</FrameLayout>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation" />
</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.DrawerLayout>
public class MainActivity extends AppCompatActivity {
private TextView mTextMessage;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
viewPager.setCurrentItem(0);
return true;
case R.id.navigation_dashboard:
viewPager.setCurrentItem(1);
return true;
case R.id.navigation_notifications:
viewPager.setCurrentItem(2);
return true;
}
return false;
}
};
private ViewPager viewPager;
private DrawerLayout drawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = findViewById(R.id.viewpager);
drawerLayout = findViewById(R.id.drawer);
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int i) {
switch (i){
case 0:
return new Fragment01();
case 1:
return new Fragment02();
case 2:
return new Fragment03();
}
return null;
}
@Override
public int getCount() {
return 3;
}
});
final BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
switch (i){
case 0:
navigation.setSelectedItemId(R.id.navigation_home);break;
case 1:
navigation.setSelectedItemId(R.id.navigation_dashboard);break;
case 2:
navigation.setSelectedItemId(R.id.navigation_notifications);break;
}
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
//吐司
drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(@NonNull View view, float v) {
}
@Override
public void onDrawerOpened(@NonNull View view) {
Toast.makeText(MainActivity.this,"侧滑打开",Toast.LENGTH_SHORT).show();
}
@Override
public void onDrawerClosed(@NonNull View view) {
Toast.makeText(MainActivity.this,"侧滑关闭",Toast.LENGTH_SHORT).show();
}
@Override
public void onDrawerStateChanged(int i) {
}
});
}
}
Utils
public class HttpUtils {
public interface CallBack {
void getData(String s) ;
}
public static void httpAsycnTask(String strUrl, final CallBack back){
new AsyncTask<String, Integer, String>() {
@Override
protected String doInBackground(String... strings) {
return getNettork(strings[0]);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
back.getData(s);
}
}.execute(strUrl);
}
public static boolean getNetwork(Context context){
if(context!=null){
ConnectivityManager manager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
if(info!= null && info.isAvailable()){
return true;
}
}
return false;
};
public static String getNettork(String strUrl){
try {
URL url = new URL(strUrl);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.setRequestMethod("GET");
InputStream stream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
StringBuilder builder = new StringBuilder();
String stt = "";
while ((stt=reader.readLine())!=null){
builder.append(stt);
}
connection.disconnect();
return builder.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
};
}
BaseActivity
public abstract class BaseActivity extends FragmentActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(bindLayout());
initView();
initData();
bindEvent();
}
protected abstract void bindEvent();
protected abstract void initData();
protected abstract void initView();
protected abstract int bindLayout();
}
下一篇: 页面自动延伸的方法