본문 바로가기

구글과인터넷/안드로이드

안드로이드 스크롤 있는 컨트롤들 자동으로 아래(밑)으로 스크롤 관련

//webview 컨드롤을 스크롤 밑으로

*방식1

 <ScrollView
         android:id="@+id/cchatview_scroll"
         android:layout_width="match_parent" 
         android:layout_height="match_parent"
            android:stackFromBottom="true"
        android:transcriptMode="alwaysScroll"
        android:layout_weight="1"
        android:background="#ffebeef2"
        >
     
        <WebView
        android:id="@+id/web"  
        android:focusable="true"
        android:focusableInTouchMode="true"
         android:layout_width="match_parent"
        android:layout_height="match_parent"

        />
      </ScrollView>

 ScrollView  _webScr  = null;
_webScr = (ScrollView)findViewById(R.id.cchatview_scroll);

이벤트_함수()
{
    _webScr.fullScroll(ScrollView.FOCUS_DOWN);
}

-단점: 포커스가 내려가나 끝까지 깔끔하게 밑으로 안내려 갈때두 있다. 
한칸 더 추가하거나 밑으로 더 내려서 움직이면 된다.


*방식2

    <LinearLayout
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     >
  <WebView
        android:id="@+id/web"  
        android:focusable="true"
        android:focusableInTouchMode="true"
         android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:stackFromBottom="true"
        android:transcriptMode="alwaysScroll"
        android:layout_weight="1"
        />
 </LinearLayout>
 
위에식으로 설정. 그냥 보통 설정으로 하셔두 됩니다. (setJavaScriptEnabled 설정을 넣어도 되고요)

WebView  _web = null;
_web = (WebView)findViewById(R.id.web);
_web.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
_web.getSettings().setJavaScriptEnabled(true); //이것 설정이 중요

final String HT_HEAD_S  = "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' /><html><head></head><body>";

final String HT_HEAD_SCREND = "<SCRIPT language=\"JavaScript\"> window.scroll(0, document.body.offsetHeight); </SCRIPT>";

final String HT_HEAD_E  = "</body></html>";


string webdata = "";
webdata = webdata + HT_HEAD_S + "(html 추가 데이터)" + HT_HEAD_SCREND + HT_HEAD_E;

_web.loadData(_webdata, "text/html", "utf-8");
또는
_web.loadDataWithBaseURL("file:///android_asset/", _webdata,"text/html", "utf-8", "file:///android_asset");

이와 같이 자바 스크립트 설정을 해두고 데이터를 읽을때 자바스크립트(HT_HEAD_SCREND)로 밑으로 스크롤을 내리게 한다.

-단점: 로드할때 마다 위에서 아래로 깜박이는듯 움직인다.

////////////////////////////////////////////////////////////////////////////////////////////////
//textview 이 scrollview 안에 설정되어있을때.

<ScrollView
         android:id="@+id/detailsScroll"
         android:layout_width="fill_parent" 
         android:layout_height="fill_parent"
        >
           <TextView 
             android:id="@+id/commentsview_cmt"
            android:gravity = "center_vertical|left"
            android:textStyle = "bold"
            android:textSize = "15sp"
            android:paddingLeft = "20dip"
            android:layout_width="fill_parent" 
             android:layout_height="fill_parent"
            android:textColor = "#ff4c566c"/>
        </ScrollView>


ScrollView scroll = (ScrollView)findViewById(R.id.detailsScroll);
scroll.post(new Runnable()
{
@Override
public void run()
{
ScrollView scroll = (ScrollView)findViewById(R.id.detailsScroll);
scroll.fullScroll(ScrollView.FOCUS_UP);

}
}); 

위에 처럼 해주거나 add할때 스크롤를 불러와서 위에 색깔처럼 코드를 넣어주자.

////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////ListView
ListView m_list;
protected MultiAdapter m_Adapter;

public void AddList(int _nkey, String leftTitle)
    {
        if(m_arItem == null || m_Adapter == null)
        {
            System.out.println("CProbCategoryList::AddList - No Create Start");
            return;
        }
        
        m_arItem.add(new ListItem(_nkey, leftTitle));
        m_list.setAdapter(m_mAdapter);
        
       
m_list.setSelection(m_list.getCount()-1); //추가 될때마다 아래쪽으로 자동 스크롤

    }



////////////////////////WebView
웹뷰 자동 아래 스크롤은 아직 정확하지 않다

public void ScrollEnd()
    {
        int contentHeight = _web.getContentHeight()+_web.getHeight(); 
        int addH = 위값으로 완전하게 내려가지 않는경우 추가해서 값을 넣어준다.
       _web.scrollTo(0, contentHeight+addH); 
    }

/////////////////////////////////////////////////////////////////////////////


EditText 를 클릭 할 때 키패드는 자동으로 올라오게된다.
이때 키패드가 화면을 가려 입력시 불편을 줄 수있다.

*XML

1  <scrollview android:layout_width="fill_parent" android:layout_height="fill_parent">
2 
3</scrollview>


//위에처럼 스크롤뷰로 감싸주면 키패드가 올라올때 스크롤이생겨서 사용자가 가려있는 뷰들을
//볼 수는 있지만..  사용자가 스크롤을 해주어야 해서 불편하다.

*Source
myEditText : 사용자가 입력하려는 EditText
myScrollView : 스크롤뷰
100 : 딜레이
0, 800 : 스크롤을 부드럽게 롤업하는 위치

01myEditText.setOnFocusChangeListener(new OnFocusChangeListener(){
02 @Override
03 public void onFocusChange(View v, boolean hasFocus) {
04  if( hasFocus == true ){
05    
06   myScrollView.postDelayed( new Runnable(){
07 
08    @Override
09    public void run() {
10     myScrollView.smoothScrollBy(0800);
11    }
12     
13   }, 100);    
14 
15  }
16 }
17 });



// 위에처럼 EditText에 포커스가 갈때 스크롤이 되게 이벤트를 주면
// 사용자가 스크롤 하지않아도 시원하게 화면이 보이게된다