80,471
社区成员




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:id="@+id/main_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/nav_view"
android:orientation="vertical">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:menu="@menu/bottom_nav_menu"
app:labelVisibilityMode="labeled"
>
</android.support.design.widget.BottomNavigationView>
</RelativeLayout>
package com.hjl.topview2;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import com.hjl.topview2.view.HomeFragment;
import com.hjl.topview2.view.MineFragment;
import com.hjl.topview2.view.ProjectFragment;
import com.hjl.topview2.view.SystemFragment;
public class MainActivity extends AppCompatActivity {
private BottomNavigationView bottomNavigationView;
private HomeFragment homeFragment;
private SystemFragment systemFragment;
private ProjectFragment projectFragment;
private MineFragment mineFragment;
private Fragment[] fragments;
private int lastFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFragment();
}
private void initFragment() {
homeFragment = new HomeFragment();
systemFragment = new SystemFragment();
projectFragment = new ProjectFragment();
mineFragment = new MineFragment();
fragments = new Fragment[]{homeFragment, systemFragment, projectFragment, mineFragment};
lastFragment = 0;
//获取Fragment管理器 -> 产生FragmentTransaction对象 ->
getSupportFragmentManager().beginTransaction().replace(R.id.main_view, homeFragment).show(homeFragment).commit();
bottomNavigationView = findViewById(R.id.nav_view);
bottomNavigationView.setOnNavigationItemSelectedListener(changeFragment);
}
private BottomNavigationView.OnNavigationItemSelectedListener changeFragment = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.navigation_home:
{
if (lastFragment != 0) {
switchFragment(lastFragment, 0);
lastFragment = 0;
}
return true;
}
case R.id.navigation_system:
{
if (lastFragment != 1) {
switchFragment(lastFragment, 1);
lastFragment = 1;
}
return true;
}
case R.id.navigation_project:
{
if (lastFragment != 2) {
switchFragment(lastFragment, 2);
lastFragment = 2;
}
return true;
}
case R.id.navigation_mine:
{
if (lastFragment != 3) {
switchFragment(lastFragment, 3);
lastFragment = 3;
}
return true;
}
}
return false;
}
};
private void switchFragment(int lastFragment, int index) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.hide(fragments[lastFragment]);
if (!fragments[index].isAdded()) {
transaction.add(R.id.main_view, fragments[index]);
}
transaction.show(fragments[index]).commitAllowingStateLoss();
}
}
<?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"
>
<android.support.v7.widget.Toolbar
android:id="@+id/home_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:fitsSystemWindows="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="@dimen/margin_norma"
android:layout_marginEnd="@dimen/margin_norma">
<TextView
android:id="@+id/home_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/title_home"
android:textColor="@color/colorWhite"
android:textSize="@dimen/toolbar_title_tx"
/>
<ImageView
android:id="@+id/home_search"
android:layout_width="@dimen/toolbar_iv_width"
android:layout_height="@dimen/toolbar_iv_height"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:contentDescription="@string/search"
android:scaleType="centerCrop"
android:src="@mipmap/search"
/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
<com.hjl.topview2.widget.RefreshLayout
android:id="@+id/home_swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/home_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
android:divider="#dddddd"
android:dividerHeight="3px"/>
</com.hjl.topview2.widget.RefreshLayout>
</LinearLayout>
getSupportFragmentManager().beginTransaction().replace(R.id.main_view, homeFragment).show(homeFragment).commit();
package com.hjl.topview2.view;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import com.hjl.topview2.R;
import com.hjl.topview2.adapter.ArticleAdapter;
import com.hjl.topview2.bean.Article;
import com.hjl.topview2.widget.RefreshLayout;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class HomeFragment extends Fragment {
private Toolbar toolbar;
private List<Article> articleList = new ArrayList<>();
private ArticleAdapter adapter;
private ListView listView;
private RefreshLayout refreshLayout;
private int pageNum = 0;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
toolbar = view.findViewById(R.id.home_toolbar);
refreshLayout = view.findViewById(R.id.home_swipeRefreshLayout);
listView = view.findViewById(R.id.home_list_view);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
adapter = new ArticleAdapter(articleList);
listView.setAdapter(adapter);
initArticle(pageNum);
// 刷新事件
refreshLayout.setOnFlushListener(new RefreshLayout.OnFlushListener() {
@Override
public void onFlush() {
articleList.clear();
pageNum = 0;
initArticle(pageNum);
//同步ListView、adapter数据
adapter.notifyDataSetChanged();
refreshLayout.setFlushing(false);
}
});
// 加载事件
refreshLayout.setOnLoadListener(new RefreshLayout.OnLoadListener() {
@Override
public void onLoad() {
pageNum++;
initArticle(pageNum);
//同步ListView、adapter数据
adapter.notifyDataSetChanged();
refreshLayout.setLoading(false);
}
});
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
public void initArticle(final int pageNum) {
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
String url1 = "https://www.wanandroid.com/article/list/";
String url2 = "/json";
String string = url1 + (pageNum - 1) +
url2;
URL url = new URL(string);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
InputStream in = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(in));
StringBuilder response = new StringBuilder();
String line;
while((line = reader.readLine()) != null) {
response.append(line);
}
parseJSONWithJSONObject(response.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (connection != null) {
connection.disconnect();
}
}
}
}).start();
}
private void parseJSONWithJSONObject(String jsonData) {
try {
JSONObject jsonObject = new JSONObject(jsonData);
JSONObject jsonObject1 = new JSONObject(jsonObject.getJSONObject("data").toString());
JSONArray jsonArray = new JSONArray(jsonObject1.getJSONArray("datas").toString());
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject2 = jsonArray.getJSONObject(i);
Article article = new Article();
article.setAuthor(jsonObject2.getString("author"));
article.setChapterId(jsonObject2.getInt("chapterId"));
article.setChapterName(jsonObject2.getString("chapterName"));
article.setCollect(jsonObject2.getBoolean("collect"));
article.setCourseId(jsonObject2.getInt("courseId"));
article.setFresh(jsonObject2.getBoolean("fresh"));
article.setId(jsonObject2.getInt("id"));
article.setLink(jsonObject2.getString("link"));
article.setNiceDate(jsonObject2.getString("niceDate"));
article.setSuperChapterId(jsonObject2.getInt("superChapterId"));
article.setSuperChapterName(jsonObject2.getString("superChapterName"));
// article.setTags(jsonObject);
article.setTitle(jsonObject2.getString("title"));
articleList.add(article);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}