欧美三级国产三级日韩三级_亚洲熟妇丰满大屁股熟妇_欧美亚洲成人一区二区三区_国产精品久久久久久模特

Android基礎(chǔ)知識——開發(fā)中遇到的問題 - 新聞資訊 - 云南小程序開發(fā)|云南軟件開發(fā)|云南網(wǎng)站建設(shè)-昆明葵宇信息科技有限公司

159-8711-8523

云南網(wǎng)建設(shè)/小程序開發(fā)/軟件開發(fā)

知識

不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價值,我們在追求其視覺表現(xiàn)的同時,更側(cè)重于功能的便捷,營銷的便利,運營的高效,讓網(wǎng)站成為營銷工具,讓軟件能切實提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序為后期升級提供便捷的支持!

您當前位置>首頁 » 新聞資訊 » 技術(shù)分享 >

Android基礎(chǔ)知識——開發(fā)中遇到的問題

發(fā)表時間:2020-10-18

發(fā)布人:葵宇科技

瀏覽次數(shù):82

文章目錄

  • 1.動態(tài)控制布局的位置
  • 2.單選按鈕的使用
  • 3.僅在需要時才顯示布局
    • 3.1控制布局的可見屬性
    • 3.2ViewStub
    • 3.3以上兩種方法的利弊
  • 4.判斷ImageView展示的是哪一張圖片
  • 5.單選框的使用
  • 6.RecyclerView實現(xiàn)側(cè)滑刪除
  • 7.在活動中處理RecyclerView的點擊事件

1.動態(tài)控制布局的位置

在開發(fā)時,我們可能需要在同一活動中的不同界面下來改變布局的位置使得布局處于一個比較合理的位置。

例如:我們在活動的主界面希望用戶能有沉浸式的體驗,所以我們將我們的布局延伸到了系統(tǒng)欄中,可是當我們轉(zhuǎn)換到設(shè)置(碎片)的界面時,就很有可能發(fā)生系統(tǒng)欄中的圖標,將我們界面上標題欄的部分按鈕遮擋的情況。而為了解決這一問題我們就要在代碼中對布局位置進行控制了。

代碼展示:

FrameLayout.LayoutParams layoutParams;
layoutParams=(FrameLayout.LayoutParams) view.findViewById(R.id.choose_layout).getLayoutParams();
if(getActivity() instanceof WeatherActivity){
    //動態(tài)控制布局的margin屬性
    layoutParams.topMargin=50;
}

2.單選按鈕的使用

在開發(fā)時,很多時候都會讓用戶選擇讓某一項功能開啟還是關(guān)閉,這個時候就需要一個比較美觀的控件來實現(xiàn)這一功能了。

效果展示:

代碼展示:

//聲明依賴
dependencies {
    implementation 'com.github.zcweng:switch-button:0.0.3'
}
//布局文件中用法
<com.suke.widget.SwitchButton
    android:id="@+id/switch_button"
    android:layout_gravity="center"
    android:layout_marginLeft="60dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
//邏輯文件中用法
switchButton.setChecked();//設(shè)置控件的開關(guān)狀態(tài)
switchButton.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(SwitchButton view, boolean isChecked) {//點擊事件
        if(isChecked==true){
            //當控件變?yōu)閠rue時的邏輯代碼
        }else if(isChecked==false){
            //當控件變?yōu)閒alse時的邏輯代碼
        }
    }
});

3.僅在需要時才顯示布局

在開發(fā)時,我們可能會需要先隱藏部分布局,然后當用戶執(zhí)行某一操作后再將這部分布局顯現(xiàn)出來。為了實現(xiàn)這一功能我給大家介紹兩種方法,并為大家指出它們的利弊。

3.1控制布局的可見屬性

我們需要先將需隱藏布局的可見屬性設(shè)置為View.GONE,然后在用戶執(zhí)行某一操作后將可見屬性設(shè)置為View.VISIBLE即可輕易實現(xiàn)這一功能。

3.2ViewStub

我們也可以使用ViewStub控件來幫助我們實現(xiàn)這一功能。

代碼展示:

//待隱藏布局
<?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" >
 
    <EditText
        android:id="@+id/edit_extra1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:hint="Extra field 1" />
 
    <EditText
        android:id="@+id/edit_extra2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="10dp"
        android:hint="Extra field 2" />
 
    <EditText
        android:id="@+id/edit_extra3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="10dp"
        android:hint="Extra field 3" />
 
</LinearLayout>
//主界面布局
<?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" >
 
    <EditText
        android:id="@+id/edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="10dp"
        android:hint="@string/edit_something_here" />
 
    <Button
        android:id="@+id/more"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="10dp"
        android:text="More" />
    
    <ViewStub 
        android:id="@+id/view_stub"
        android:layout="@layout/profile_extra"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
 
</LinearLayout>
//邏輯代碼
private EditText editExtra1;
private EditText editExtra2;
private EditText editExtra3;

public void onMoreClick() {//當我們點擊more按鈕時執(zhí)行該函數(shù),即可是實現(xiàn)隱藏布局的顯示
	ViewStub viewStub = (ViewStub) findViewById(R.id.view_stub);
	if (viewStub != null) {
		View inflatedView = viewStub.inflate();
		editExtra1 = (EditText) inflatedView.findViewById(R.id.edit_extra1);
		editExtra2 = (EditText) inflatedView.findViewById(R.id.edit_extra2);
		editExtra3 = (EditText) inflatedView.findViewById(R.id.edit_extra3);
	}

3.3以上兩種方法的利弊

第一種方法:

  • 優(yōu)點:邏輯簡單而且控制起來比較靈活。

  • 缺點:耗費資源。雖然把View的初始可見View.GONE但是在Inflate布局的時候View仍然會被Inflate,也就是說仍然會創(chuàng)建對象,會被實例化,會被設(shè)置屬性。也就是說,會耗費內(nèi)存等資源。

第二種方法:

  • 優(yōu)點:在Inflate布局的時候,只有ViewStub會被初始化,然后當ViewStub被設(shè)置為可見的時候,或是調(diào)用了ViewStub.inflate()的時候,ViewStub所向的布局就會被Inflate和實例化,然后ViewStub的布局屬性都會傳給它所指向的布局。這樣,就可以使用ViewStub來方便的在運行時,要還是不要顯示某個布局。這樣也就節(jié)省了內(nèi)存資源,實現(xiàn)了布局的優(yōu)化。

  • 特點:ViewStub只能Inflate一次,之后ViewStub對象會被置為空。也就是說,某個被ViewStub指定的布局被Inflate后,就不能再通過ViewStub來控制它了;ViewStub只能用來Inflate一個布局文件,而不是某個具體的View,當然也可以把View寫在某個布局文件中。

  • 缺點:在程序的運行期間,某個布局在Inflate后,就不會有變化,除非重新啟動。也就是說,不能再將加載出來的布局隱藏了;只能控制顯示與隱藏的是布局文件,而非某個View。

4.判斷ImageView展示的是哪一張圖片

在開發(fā)時,我們有可能要根據(jù)ImageView展示的圖片,來決定讓其執(zhí)行哪一點擊事件。而要實現(xiàn)這一功能就需要判斷先ImageView展示的是哪一張圖片。

代碼展示:

moreImage.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if(moreImage.getDrawable().getCurrent().getConstantState().equals(ContextCompat.getDrawable(SettingActivity.this,R.drawable.many).getConstantState())){//判斷方法
            //具體邏輯
        }else {
            //具體邏輯
        }
    }
});

5.單選框的使用

在開發(fā)時,我們有時需要提供一組選項,來讓用戶選擇一個。而實現(xiàn)這一功能就要利用單選框控件了。

代碼展示:

//布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <RadioGroup
        android:id="@+id/radio_group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <RadioButton
            android:id="@+id/eight_time"
            android:layout_margin="10dp"
            android:text="8小時"
            android:textSize="20sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <RadioButton
            android:id="@+id/four_time"
            android:layout_margin="10dp"
            android:text="4小時"
            android:textSize="20sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <RadioButton
            android:id="@+id/two_time"
            android:layout_margin="10dp"
            android:text="2小時"
            android:textSize="20sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <RadioButton
            android:id="@+id/one_time"
            android:layout_margin="10dp"
            android:text="1小時"
            android:textSize="20sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </RadioGroup>

</LinearLayout>
//邏輯代碼
radioGroup.check();//設(shè)置哪一單選框為選中狀態(tài)
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {//點擊事件
    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i){
            case R.id.eight_time:
                //選中后的邏輯代碼
                break;
            case R.id.four_time:
                //選中后的邏輯代碼
                break;
            case R.id.two_time:
                 //選中后的邏輯代碼
                break;
            case R.id.one_time:
                 //選中后的邏輯代碼
                break;
            default:
        }
    }
}

6.RecyclerView實現(xiàn)側(cè)滑刪除

在開發(fā)中,我們有時需要實現(xiàn)側(cè)滑RecyclerView的某一子項布局來刪除它的功能。

以下代碼中省略了RecyclerView的基本實現(xiàn)方法,倘若有讀者還不太清楚,請移步這里

代碼展示:

//為RecycleView綁定觸摸事件
ItemTouchHelper helper=new ItemTouchHelper(new ItemTouchHelper.Callback() {

    @Override
    public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
    	//首先回調(diào)的方法 返回int表示是否監(jiān)聽該方向
        int dragFlags = ItemTouchHelper.UP|ItemTouchHelper.DOWN;//拖拽
        int swipeFlags = ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT;//側(cè)滑刪除
        return makeMovementFlags(dragFlags,swipeFlags);
    }
    
    @Override
    public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
    	//滑動事件
        return false;
    }
    
    @Override
    public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
    	//側(cè)滑事件
        nameList.remove(viewHolder.getAdapterPosition());
        myAdapter.notifyItemRemoved(viewHolder.getAdapterPosition());
    }
    
});
helper.attachToRecyclerView(recyclerView);

注意:倘若你還需在側(cè)滑事件中刪除數(shù)據(jù)庫的內(nèi)容,要將其放在remove函數(shù)之前,否則getAdapterPosition()的返回值會發(fā)生變化。

7.在活動中處理RecyclerView的點擊事件

當我們在處理RecyclerView的點擊事件時很有可能還要用到活動中的一些數(shù)據(jù),而一般情況下RecyclerView的點擊事件是在其適配器中注冊的,這就使得你可能無法在點擊事件中實現(xiàn)某些功能了。而為了解決在活動中處理點擊事件這一問題,我們可以利用回調(diào)接口來實現(xiàn)。

代碼展示:

//定義接口
public interface>{
    void onItemClick(int position);
}
//在適配器的點擊事件中調(diào)用該接口中的方法
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private List<Saved> list=new ArrayList<>();
    private>=null;
    
    public MyAdapter(List<Saved> list,>){//我們還需要在構(gòu)造方法傳入該接口
        this.Listener=Listener;
        this.list=list;
    }
    
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, final int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item,parent,false);
        final ViewHolder viewHolder=new ViewHolder(view);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Listener.onItemClick(viewHolder.getAdapterPosition());//調(diào)用接口中的方法
            }
        });
        return viewHolder;
    }

	......
}
//在活動中實現(xiàn)該接口中的方法
OnItemClickListener recyclerListener=new OnItemClickListener() {
    @Override
    public void onItemClick(int position) {
        //具體邏輯代碼
    }
};

相關(guān)案例查看更多