2016年2月25日 星期四

Android 解決 繼承DrawerActivity 時 ListView 互搶焦點


DrawerActivity   裡面本身就有ListView   Activity 又有一個 只要開啟 DrawerActivity  
Activity 內的ListView 焦點一定會被搶走
Activity 的ListView 又有搭配ScrollView 每次打開 關閉 都會被推到頂部
這是因為焦點被搶走
只要設定Activiy 的焦點 不要消失
設定 DrawerActivity  ListView  open的時候 移除焦點 就可以暫時解決問題了

code

Activity

  @Override
    protected void onPause() {
        super.onPause();
       
        mListView.setFocusable(true);
    }

    @Override
    protected void onResume() {
        super.onResume();

    
        mListView.setFocusable(true);
    }

DrawerActivity 

 public void open()
    {
        drawerLayout.openDrawer(drawerList);
        drawerList.setFocusable(false);
    }
  

2016年2月18日 星期四

json格式 撈取 使用方法


首先 先使用下面這網址

先 將全部的 縮牌縮起來
會變成
{}
這樣意思是 JSONObject
所以要先用JSONObject o = new JSONObject(result); 做接口
之後

{
  • "success":true,
  • "result":
}
紅色也是JSONObject 但前面有"result
等於 芝麻開門要有密碼
所以就要 JSONObject resultObj = o.getJSONObject("result");
去開通 這一層
  • "result":{
    • "resource_id":"c900ce98-0089-47cc-b044-ce87235078b4",
    • "fields":,
    • "records":,
    • "total":55,
    • "limit":100
    }
  • 然後我們要排的 變成[] 這是 jsonarray 
  • 跟前面一樣 所以要通關密碼
 JSONArray jsonArray = resultObj.getJSONArray("records");

這樣 就可以排到想要的資料了

2016年2月17日 星期三

ListView in ScrollView Demo 2


public class MyScrollView extends ScrollView {
    private Runnable scrollerTask;
    private int initialPosition;

    private int newCheck = 100;
    private static final String TAG = "MyScrollView";

    public interface OnScrollStoppedListener{
        void onScrollStopped();
    }
    public interface OnScrollListener{
        void onScroll();
    }

    private OnScrollStoppedListener onScrollStoppedListener;
    private OnScrollListener onScrollListener;

    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);

        scrollerTask = new Runnable() {

            public void run() {

                int newPosition = getScrollY();
                if(initialPosition - newPosition == 0){//has stopped
                    if(onScrollStoppedListener!=null){

                        onScrollStoppedListener.onScrollStopped();
                    }
                }else{
                    initialPosition = getScrollY();
                    MyScrollView.this.postDelayed(scrollerTask, newCheck);
                }
            }
        };
    }

    @Override    public boolean onTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_UP) {
            startScrollerTask();
        }
        return super.onTouchEvent(ev);
    }

    @Override    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);

        if (onScrollListener != null) {
            onScrollListener.onScroll();
        }
    }

    public void setOnScrollStoppedListener(MyScrollView.OnScrollStoppedListener listener){
        onScrollStoppedListener = listener;
    }
    public void setOnScrollListener(MyScrollView.OnScrollListener listener){
        onScrollListener = listener;
    }

    public void startScrollerTask(){

        initialPosition = getScrollY();
        MyScrollView.this.postDelayed(scrollerTask, newCheck);
    }

}

Xml

ScrollView to  com.XXXX.ScrollView

Activity
private MyScrollView sv;
sv = (MyScrollView) findViewById(R.id.eee);

        sv.setOnScrollListener(new MyScrollView.OnScrollListener() {
            private int lastY = sv.getScrollY();
            private int touchEventId = -9983761;
            Handler handler = new Handler() {
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    if (msg.what == touchEventId) {
                        if (lastY != sv.getScrollY()) {
                            //scrollview一直在滚动,会触发                            handler.sendMessageDelayed(
                                    handler.obtainMessage(touchEventId, sv), 5);
                            lastY = sv.getScrollY();
//                            tvdong.getLocationOnScreen(location);                            mLayout_couponstore.getLocationOnScreen(location);
//                            tvjing.getLocationOnScreen(location2);                            mLayout_couponstore2.getLocationOnScreen(location2);
                            //动的到静的位置时,静的显示。动的实际上还是网上滚动,但我们看到的是静止的那个                            if (location[1] <= location2[1]) {
//                                tvjing.setVisibility(View.VISIBLE);                                mLayout_couponstore2.setVisibility(View.VISIBLE);
                            } else {
                                //静止的隐藏了//                                tvjing.setVisibility(View.GONE);                                mLayout_couponstore2.setVisibility(View.GONE);
                            }
                        }
                    }
                }
            };
            @Override            public void onScroll() {

                handler.sendMessageDelayed(
                        handler.obtainMessage(touchEventId, sv), 5);
            }
        });

if use
ListViewForScrollView.setListViewHeightBasedOnChildren(mListstore);

plesae add this for after
sv.smoothScrollTo(0,0);




2016年2月15日 星期一

Android ListView In ListView 可以放在Listview中的Listview



public class InnerListview extends ListView {

public InnerListview(Context context) {
super(context);
}

public InnerListview(Context context, AttributeSet attrs) {
super(context, attrs);
}

public InnerListview(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);

}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
// 当手指触摸listview时,让父控件交出ontouch权限,不能滚动
case MotionEvent.ACTION_DOWN:
setParentScrollAble(false);
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
// 当手指松开时,让父控件重新获取onTouch权限
setParentScrollAble(true);
break;

}
return super.onInterceptTouchEvent(ev);

}

// 设置父控件是否可以获取到触摸处理权限
private void setParentScrollAble(boolean flag) {
getParent().requestDisallowInterceptTouchEvent(!flag);
}

}

Android ListView For ScrollView


public class ListViewForScrollView  extends ListView{
    public ListViewForScrollView(Context context) {
        super(context);
    }

    public ListViewForScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ListViewForScrollView(Context context, AttributeSet attrs,
                                 int defStyle) {
        super(context, attrs, defStyle);

    }

    /**     * 只需要重写这个方法即可     */    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }

    /**     * scrollview嵌套listview显示不全解决     *     * @param listView     */    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight
                + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }


}



MainActivity
mListstore = (ListView) findViewById(R.id.liststore);
ListViewForScrollView.setListViewHeightBasedOnChildren(mListstore);


這邊要注意的是

如果 listview item 顯示多少 就只會滑動多少

2016年2月4日 星期四

Android 控件随ScrollView滾動到某位置会停靠的效果


Activity
--------------------------------------------------------------------------------
import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;
import android.widget.TextView;

public class CouponStoreActivity extends Activity {
    private TextView tvdong;
    private TextView tvjing;
    private ScrollView sv;
    int[] location = new int[2];
    int[] location2 = new int[2];
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coupon_store);
        initview();
    }
    private void initview() {
        //在滚动的一个TextView        tvdong = (TextView) findViewById(R.id.dong);
        //一开始是View.GONE的一个静止的TextView        tvjing = (TextView) findViewById(R.id.jing);
        sv = (ScrollView) findViewById(R.id.eee);
        tvjing.setVisibility(View.GONE);

        sv.setOnTouchListener(new View.OnTouchListener() {
            private int lastY = 0;
            private int touchEventId = -9983761;
            Handler handler = new Handler() {
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    if (msg.what == touchEventId) {
                        if (lastY != sv.getScrollY()) {
                            //scrollview一直在滚动,会触发                            handler.sendMessageDelayed(
                                    handler.obtainMessage(touchEventId, sv), 5);
                            lastY = sv.getScrollY();
                            tvdong.getLocationOnScreen(location);
                            tvjing.getLocationOnScreen(location2);
                            //动的到静的位置时,静的显示。动的实际上还是网上滚动,但我们看到的是静止的那个                            if (location[1] <= location2[1]) {
                                tvjing.setVisibility(View.VISIBLE);
                            } else {
                                //静止的隐藏了                                tvjing.setVisibility(View.GONE);
                            }
                        }
                    }
                }
            };

            public boolean onTouch(View v, MotionEvent event) {
                //必须两个都搞上,不然会有瑕疵。                //没有这段,手指按住拖动的时候没有效果                if (event.getAction() == MotionEvent.ACTION_MOVE) {
                    handler.sendMessageDelayed(
                            handler.obtainMessage(touchEventId, v), 5);
                }
                //没有这段,手指松开scroll继续滚动的时候,没有效果                if (event.getAction() == MotionEvent.ACTION_UP) {
                    handler.sendMessageDelayed(
                            handler.obtainMessage(touchEventId, v), 5);
                }
                return false;
            }
        });
    }
}

Xml
------------------------------------------------------------




<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    tools:context=".MainActivity" >

    <ScrollView        android:id="@+id/eee"        android:layout_width="fill_parent"        android:layout_height="wrap_content" >

        <LinearLayout            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:orientation="vertical" >

            <TextView                android:layout_width="fill_parent"                android:layout_height="wrap_content"                android:background="#FF8800"                android:text="\n  \n  \n      \n \n\n\n\n" />

            <TextView                android:id="@+id/dong"                android:layout_width="fill_parent"                android:layout_height="wrap_content"                android:background="#008833"                android:text="\n\n这  是   滚  动    的     等  一  下  固  定  在 上  面\n\n" />

            <TextView                android:layout_width="fill_parent"                android:layout_height="wrap_content"                android:background="#00ff33"                android:text="~~~~~~\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n~~~~~~~" />
        </LinearLayout>
    </ScrollView>

    <TextView        android:id="@+id/jing"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:background="#008800"        android:text="\n\n这    是   固     定     的\n\n" />
</FrameLayout>