Files
assistant-android/app/src/main/java/com/gh/gamecenter/SubjectActivity.java
CsHeng b3d63c5698 1、处理viewholder
2、处理adapter
3、
2017-06-16 15:39:45 +08:00

202 lines
7.1 KiB
Java

package com.gh.gamecenter;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.gh.base.BaseActivity;
import com.gh.common.util.EntranceUtils;
import com.gh.gamecenter.adapter.VPFragmentAdapter;
import com.gh.gamecenter.entity.SubjectHeadEntity;
import com.gh.gamecenter.retrofit.JSONObjectResponse;
import com.gh.gamecenter.retrofit.Response;
import com.gh.gamecenter.retrofit.RetrofitManager;
import com.gh.gamecenter.subject.SubjectFragment;
import com.gh.gamecenter.subject.SubjectTileFragment;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import retrofit2.HttpException;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
/**
* Created by khy on 2017/4/29.
*/
public class SubjectActivity extends BaseActivity {
@BindView(R.id.subject_viewpager)
ViewPager mViewPager;
@BindView(R.id.subject_tab)
TabLayout mTabLayout;
@BindView(R.id.reuse_ll_loading)
LinearLayout mLoading;
@BindView(R.id.subject_tiled)
FrameLayout mSubjectTiled;
@BindView(R.id.reuse_no_connection)
View mNoConn;
private String mId;
private String mName;
private Bundle mBundle;
private SubjectTileFragment mTileFragment;
/**
* 启动专题页面
*/
public static void startSubjectActivity(Context context, String id, String name, boolean isOrder, String entrance) {
Intent intent = new Intent(context, SubjectActivity.class);
intent.putExtra("id", id);
intent.putExtra("name", name);
intent.putExtra("order", isOrder);
intent.putExtra(EntranceUtils.KEY_ENTRANCE, entrance);
context.startActivity(intent);
}
@Override
protected int getLayoutId() {
return R.layout.activity_subject;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBundle = getIntent().getExtras();
if (getIntent().getBundleExtra("data") != null) {
mBundle = getIntent().getBundleExtra("data");
}
mId = mBundle.getString("id");
mName = mBundle.getString("name");
initTitle(mName);
if (TextUtils.isEmpty(mName) && !TextUtils.isEmpty(mId)) {
getSubjectName(mId);
} else {
loadSubjectType();
}
mNoConn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mNoConn.setVisibility(View.GONE);
mLoading.setVisibility(View.VISIBLE);
loadSubjectType();
}
});
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (mTileFragment != null) {
mTileFragment.onTouchEvent(ev);
}
return super.dispatchTouchEvent(ev);
}
private void getSubjectName(String id) {
RetrofitManager.getApi()
.getSubjectName(id)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new JSONObjectResponse() {
@Override
public void onResponse(JSONObject response) {
try {
String name = response.getString("name");
TextView actionbar_tv_title = (TextView) findViewById(R.id.actionbar_tv_title);
actionbar_tv_title.setText(name);
mBundle.putString("name", name);
loadSubjectType();
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(HttpException e) {
super.onFailure(e);
TextView actionbar_tv_title = (TextView) findViewById(R.id.actionbar_tv_title);
actionbar_tv_title.setText("专题");
mBundle.putString("name", "专题");
loadSubjectType();
}
});
}
private void loadSubjectType() {
RetrofitManager
.getApi()
.getColumnSettings(mId)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<SubjectHeadEntity>() {
@Override
public void onResponse(SubjectHeadEntity response) {
super.onResponse(response);
mLoading.setVisibility(View.GONE);
List<String> content = response.getTypeEntity().getContent();
content.add(0, "全部");
initView(response);
}
@Override
public void onFailure(HttpException e) {
super.onFailure(e);
mLoading.setVisibility(View.GONE);
mNoConn.setVisibility(View.VISIBLE);
}
});
}
private void initView(SubjectHeadEntity headEntity) {
if ("tile".equals(headEntity.getTypeEntity().getLayout())) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Bundle clone = (Bundle) mBundle.clone();
clone.putString("tagType", headEntity.getTag());
clone.putStringArrayList("contentTitle", new ArrayList<>(headEntity.getTypeEntity().getContent()));
mTileFragment = new SubjectTileFragment();
mTileFragment.setArguments(clone);
transaction.add(R.id.subject_tiled, mTileFragment);
transaction.commit();
} else {
List<String> tag = headEntity.getTypeEntity().getContent();
if (tag.size() > 1) {
mTabLayout.setVisibility(View.VISIBLE);
}
List<Fragment> fragments = new ArrayList<>();
for (String s : tag) {
mTabLayout.addTab(mTabLayout.newTab().setText(s));
Bundle clone = (Bundle) mBundle.clone();
clone.putString("type", s);
clone.putString("tagType", headEntity.getTag());
fragments.add(SubjectFragment.newInstance(clone));
}
VPFragmentAdapter adapter =
new VPFragmentAdapter(getSupportFragmentManager(), fragments, tag);
mViewPager.setAdapter(adapter);
mTabLayout.setupWithViewPager(mViewPager);
// mTabLayout.setTabsFromPagerAdapter(adapter);
}
}
}