Skip to content

Commit

Permalink
add more way to addview
Browse files Browse the repository at this point in the history
  • Loading branch information
wangpeng committed Jun 5, 2017
1 parent 9b1be49 commit acdd2c0
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 17 deletions.
44 changes: 40 additions & 4 deletions demo/src/main/java/name/gudong/demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

Expand All @@ -41,11 +44,17 @@ public class MainActivity extends AppCompatActivity {
LinearLayout mLlContainer;
@Bind(R.id.pb)
ProgressBar mPb;
@Bind(R.id.root_layout)
FrameLayout mRootLayout;

private ViewController<List<String>> mHousePhotoViewController;
private ViewController<HouseDetail.Param> mHouseParamViewControler;
private ViewController<List<String>> mHouseCommentViewControler;
private ViewController<String> mHouseDescViewControler;
private ViewController<List<String>> mHousePhotoViewController;
private ViewController<HouseDetail.Param> mHouseParamViewControler;

private ViewController<HouseDetail.Param> mHouseParamTopViewControler;
private ViewController<HouseDetail.Param> mHouseParamBottomViewControler;

private ViewController<List<String>> mHouseCommentViewControler;
private ViewController<String> mHouseDescViewControler;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -55,6 +64,18 @@ protected void onCreate(Bundle savedInstanceState) {

mHousePhotoViewController = new HousePhotoViewController(this);
mHouseParamViewControler = new HouseParamViewController(this);


mHouseParamTopViewControler = new HouseParamViewController(this);
mHouseParamTopViewControler.attachRoot(mRootLayout, Utils.dp2px(200), Utils.dp2px(50));


mHouseParamBottomViewControler = new HouseParamViewController(this);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
mHouseParamBottomViewControler.attachRoot(mRootLayout, layoutParams);


mHouseDescViewControler = new HouseDescViewController(this);
mHouseCommentViewControler = new HouseCommentViewController(this);

Expand All @@ -69,6 +90,11 @@ protected void onCreate(Bundle savedInstanceState) {
private void fillData(HouseDetail detail) {
mHousePhotoViewController.fillData(detail.photos);
mHouseParamViewControler.fillData(detail.param);

mHouseParamTopViewControler.fillData(detail.param);
mHouseParamBottomViewControler.fillData(detail.param);


mHouseDescViewControler.fillData(detail.desc);
mHouseCommentViewControler.fillData(detail.comments);
}
Expand Down Expand Up @@ -123,4 +149,14 @@ private HouseDetail mockHouseData() {
return detail;
}

@Override
protected void onDestroy() {
super.onDestroy();
mHousePhotoViewController.detachedRoot();
mHouseParamViewControler.detachedRoot();
mHouseDescViewControler.detachedRoot();
mHouseCommentViewControler.detachedRoot();
mHouseParamTopViewControler.detachedRoot();
mHouseParamBottomViewControler.detachedRoot();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package name.gudong.demo.view;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
Expand All @@ -43,8 +45,11 @@ public class HousePhotoViewController extends ViewController<List<String>> {
@Bind(R.id.view_page)
ViewPager mViewPage;
private PhotoAdapter mAdapter;
private Handler mHandler;

public HousePhotoViewController(Context context) {
super(context);
mHandler = new Handler(Looper.getMainLooper());
}

@Override
Expand All @@ -54,35 +59,49 @@ protected int resLayoutId() {

@Override
protected void onCreatedView(View view) {
ButterKnife.bind(this,view);
ButterKnife.bind(this, view);
mViewPage = (ViewPager) view.findViewById(R.id.view_page);
mAdapter = new PhotoAdapter();
mAdapter = new PhotoAdapter(getContext());
mViewPage.setAdapter(mAdapter);
}

@Override
protected void onBindView(List<String> data) {
mAdapter.fillData(data);
mHandler.postDelayed(new Runnable() {
@Override
public void run() {

}
}, 500);
}

@Override
protected void onDestoryView(View view) {
super.onDestoryView(view);
mHandler.removeCallbacksAndMessages(null);
}

class PhotoAdapter extends PagerAdapter{
static class PhotoAdapter extends PagerAdapter {

List<String>mPhotos;
List<View>mPhotoViews ;
List<String> mPhotos;
List<View> mPhotoViews;
Context context;

public PhotoAdapter() {
public PhotoAdapter(Context context) {
mPhotos = new ArrayList<>();
mPhotoViews = new ArrayList<>();
this.context = context;
}

void fillData(List<String>photos){
void fillData(List<String> photos) {
this.mPhotos = photos;
notifyDataSetChanged();
}

@Override
public int getCount() {
Logger.i("size "+mPhotos.size());
Logger.i("size " + mPhotos.size());
return mPhotos.size();
}

Expand All @@ -98,9 +117,9 @@ public void destroyItem(ViewGroup view, int position, Object object) {

@Override
public View instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(getContext());
ImageView imageView = new ImageView(context);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
Picasso.with(getContext()).load(mPhotos.get(position)).into(imageView);
Picasso.with(context).load(mPhotos.get(position)).into(imageView);
container.addView(imageView);
mPhotoViews.add(imageView);
return imageView;
Expand Down
9 changes: 9 additions & 0 deletions demo/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context="name.gudong.demo.view.MainActivity">

</android.support.constraint.ConstraintLayout>
1 change: 1 addition & 0 deletions demo/src/main/res/layout/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f9f5f5"
android:id="@+id/root_layout"
android:orientation="vertical">

<ScrollView
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* core of view controler
* Created by GuDong on 7/13/16 22:42.
* Contact with gudong.name@gmail.com.
*
* @param <T> data type for this ViewController, a view controler should map a data type
*/
public abstract class ViewController<T> {
Expand All @@ -38,13 +39,45 @@ public ViewController(Context context) {
}

public void attachRoot(ViewGroup root) {
initView(root);
root.addView(mView);
onCreatedView(mView);
}

public void attachRoot(ViewGroup root, int index) {
initView(root);
root.addView(mView, index);
onCreatedView(mView);
}

public void attachRoot(ViewGroup root, int width, int height) {
initView(root);
root.addView(mView, width, height);
onCreatedView(mView);
}

public void attachRoot(ViewGroup root, ViewGroup.LayoutParams params) {
initView(root);
root.addView(mView, params);
onCreatedView(mView);
}


public void attachRoot(ViewGroup root, int index, ViewGroup.LayoutParams params) {
initView(root);
root.addView(mView, index, params);
onCreatedView(mView);
}

private void initView(ViewGroup root) {
int resLayoutId = resLayoutId();
if (resLayoutId <= 0) {
throw new IllegalStateException("Please check your layout id in resLayoutId() method");
}
if (mView != null) {
throw new IllegalStateException("a viewController can't attachRoot twice");
}
mView = LayoutInflater.from(mContext).inflate(resLayoutId, root, false);
root.addView(mView);
onCreatedView(mView);
}

public void fillData(T data) {
Expand All @@ -54,6 +87,10 @@ public void fillData(T data) {
}
}

public void detachedRoot() {
onDestoryView(mView);
}

/**
* indicate layout id of this ViewControl
*
Expand All @@ -62,7 +99,7 @@ public void fillData(T data) {
protected abstract int resLayoutId();

/**
* view has been
* view has been created
*
* @param view real view
*/
Expand All @@ -75,6 +112,15 @@ public void fillData(T data) {
*/
protected abstract void onBindView(T data);

/**
* view has been Destory
*
* @param view
*/
protected void onDestoryView(View view) {

}

public Context getContext() {
return mContext;
}
Expand Down

0 comments on commit acdd2c0

Please sign in to comment.