Androidc学习笔记二之四大布局及碎片理解用法

android中四大布局、ListView的用法和理解、RecyclerView的用法和理解、android中碎片的理解和用法

android中的四大布局;

LinearLayout(流式布局)  其中学到layout_weight权重值概念权重值概念
relativeLayout(相对布局) 其中layout_alignParentTop/right/left/botton="true"....
                         layout_above/below="@id/button1"  layout_toright/left="@id/button1"
FrameLayout(帧布局) 应用场景太少,基本就是固定模式主要用到gravity(定重心即可)
PercentFrameLayout(百分比布局) 为了解决百分比占用布局的问题而开发出来的,
 首先这个需要引库在app/build.gradle文件,dependencies比包下添加内容compile'com.android.support:percent:24.2.1'即可,需要同步一下(Sync Now)
用到layout_gravity、layout_width/heightPercent="40%"来运用百分比。
TableLayout表格布局  AbsoluteLayout绝对布局


可以发现我们再android中使用的空间基本都是直接或者间接继承View的,当然除了自定义控件。


ListView控件:最常用也是最难的控件,主要用于数据的显示(主要是android的屏幕空间能够显示的内容太少了)
 ListView用户允许用户上下滑动来展示数据。
 通过构建适配器Adapter来构建索要显示的内容最后将适配器设置(ListView.setAdapter(adapter))到listview上去就搭建成功了。

例子:

activity_main.xml



    

    


fruit_item.xml




    

    

FruitBean.class
package com.example.ldp.com.bean;

/**
 * Created by Administrator on 2017/3/6.
 */

public class FruitBean {
    private String name;
    private int imageid;

    public String getName() {
        return name;
    }

    public FruitBean(String name,int imageid){
        this.name = name;
        this.imageid = imageid;
    }

    public int getImageid() {
        return imageid;
    }

}

FruitAdapter.class
package com.example.ldp.com.bean;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.ldp.com.uicustomerviews.R;

import java.util.List;

/**
 * Created by Administrator on 2017/3/6.
 */

public class FruitAdapter extends ArrayAdapter {
    private int resourceId;
    public FruitAdapter(Context context, int textViewResourceId, List objects) {
        super(context, textViewResourceId, objects);
        resourceId = textViewResourceId;
    }


    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    //convertView用于将之前加载好的布局进行缓存,以便可以重用

        FruitBean fruit = getItem(position);//获取当前项的fruit实例

        View view;
        ViewHolder viewholder;
        if(convertView==null){
            view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
            viewholder = new ViewHolder();
            viewholder.fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
            viewholder.fruitName = (TextView)view.findViewById(R.id.fruit_name);
            view.setTag(viewholder);//将viewHolder存储在View中
        }else{
            view = convertView;//所以当不为空的时候就重用,深化性能
            viewholder = (ViewHolder) view.getTag();//重新获取ViewHolder
        }

        viewholder.fruitImage.setImageResource(fruit.getImageid());
        viewholder.fruitName.setText(fruit.getName());
        return view;
    }

    class ViewHolder{
        /*第一步是缓存布局converView,第二步是缓存控件,当这个布局存在的情况下
        ,自然其中的控件也存在就不用每次都通过findViewById来找实例了,通过这两步实现这个ListView性能优化*/
        ImageView fruitImage;
        TextView fruitName;
    }
}

MainActivity.class
package com.example.ldp.com.uicustomerviews;

import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.example.ldp.com.bean.FruitAdapter;
import com.example.ldp.com.bean.FruitBean;

import java.util.ArrayList;
import java.util.List;

import static android.widget.AdapterView.*;

public class MainActivity extends AppCompatActivity {



    private List fruitList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        initFruits();//初始化数据
        FruitAdapter adapter = new FruitAdapter(MainActivity.this,R.layout.fruit_item,fruitList);
        ListView listView = (ListView) findViewById(R.id.list_view);
        listView.setAdapter(adapter);

        //相应listView子布局项的响应(监听event)
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                FruitBean fruit =fruitList.get(position);
                Toast.makeText(MainActivity.this,fruit.getName()+"您点击了!",Toast.LENGTH_SHORT).show();
            }
        });
    }


    private void initFruits(){
        for(int i=0;i<2;i++){
            FruitBean apple = new FruitBean("Apple",R.drawable.ic_launcher);
            fruitList.add(apple);
            FruitBean apple1 = new FruitBean("Apple",R.drawable.ic_launcher);
            fruitList.add(apple1);
            FruitBean apple2 = new FruitBean("Apple",R.drawable.ic_launcher);
            fruitList.add(apple2);
            FruitBean apple3 = new FruitBean("Apple",R.drawable.ic_launcher);
            fruitList.add(apple3);
            FruitBean apple4 = new FruitBean("Apple",R.drawable.ic_launcher);
            fruitList.add(apple4);
            FruitBean apple5 = new FruitBean("Apple",R.drawable.ic_launcher);
            fruitList.add(apple5);
            FruitBean apple6 = new FruitBean("Apple",R.drawable.ic_launcher);
            fruitList.add(apple6);
            FruitBean apple7 = new FruitBean("Apple",R.drawable.ic_launcher);
            fruitList.add(apple7);
            FruitBean apple8 = new FruitBean("Apple",R.drawable.ic_launcher);
            fruitList.add(apple8);
            FruitBean apple9 = new FruitBean("Apple",R.drawable.ic_launcher);
            fruitList.add(apple9);

        }
    }
}

ListView效果图:
                                           Androidc学习笔记二之四大布局及碎片理解用法_第1张图片


RecycleView(滚动控件):用它的理由自然是因为ListView的缺点了,
它只能纵向滑动,不能横向滑动,而且又性能缺点,只能采用一些技巧来优化。
当我们需要实现横向功能的时候,就需要用来RecycleView这种滚动控件了。
它即可轻松实现ListView的额工恩能够,也优化了许多的不足。

注:它和百分比一样需要引库才能使用。

build.gradle文件			
dependencies {
    compile 'com.android.support:recyclerview-v7:25.2.0'
}


activity_main.xml




    

fruit_item.xml




    

    


Fruit.class
package com.example.ldp.com.bean;

/**
 * Created by Administrator on 2017/3/6.
 */

public class Fruit {
    private String name;
    private int imageid;

    public String getName() {
        return name;
    }

    public Fruit(String name,int imageid){
        this.name = name;
        this.imageid = imageid;
    }

    public int getImageid() {
        return imageid;
    }
}

FruitAdapter.class
package com.example.ldp.com.bean;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.ldp.com.recyclerview.R;

import java.util.List;

/**
 * Created by Administrator on 2017/3/6.
 */

public class FruitAdapter extends RecyclerView.Adapter {


    private List mfruitLists;
    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView fruitImage;
        TextView fruitName;
        public ViewHolder(View View){
            super(View);
            fruitImage =(ImageView) View.findViewById(R.id.fruit_image);
            fruitName = (TextView) View.findViewById(R.id.fruit_name);
        }
    }

    public FruitAdapter(List fruitList){
        this.mfruitLists=fruitList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item,parent,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Fruit fruit = mfruitLists.get(position);
        holder.fruitImage.setImageResource(fruit.getImageid());
        holder.fruitName.setText(fruit.getName());
    }

    @Override
    public int getItemCount() {

        return mfruitLists.size();
    }


}

MainActivity.class
package com.example.ldp.com.recyclerview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.example.ldp.com.bean.Fruit;
import com.example.ldp.com.bean.FruitAdapter;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private List fruitList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initFruits();//初始化数据

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        FruitAdapter adapter = new FruitAdapter(fruitList);
        recyclerView.setAdapter(adapter);

        //监听

    }


    private void initFruits(){
        for(int i=0;i<2;i++){
            Fruit apple = new Fruit("Apple",R.drawable.ic_launcher);//照片和内容
            fruitList.add(apple);
            Fruit apple1 = new Fruit("Apple",R.drawable.ic_launcher);
            fruitList.add(apple1);
            Fruit apple2 = new Fruit("Apple",R.drawable.ic_launcher);
            fruitList.add(apple2);
            Fruit apple3 = new Fruit("Apple",R.drawable.ic_launcher);
            fruitList.add(apple3);
            Fruit apple4 = new Fruit("Apple",R.drawable.ic_launcher);
            fruitList.add(apple4);
            Fruit apple5 = new Fruit("Apple",R.drawable.ic_launcher);
            fruitList.add(apple5);
            Fruit apple6 = new Fruit("Apple",R.drawable.ic_launcher);
            fruitList.add(apple6);
            Fruit apple7 = new Fruit("Apple",R.drawable.ic_launcher);
            fruitList.add(apple7);
            Fruit apple8 = new Fruit("Apple",R.drawable.ic_launcher);
            fruitList.add(apple8);
            Fruit apple9 = new Fruit("Apple",R.drawable.ic_launcher);
            fruitList.add(apple9);

        }
    }
}

这个的效果图个ListView差不多它就是为了实现ListView的效果而演示的。

若是要将recycleList修改成横向滑动的话只需要在刚才的代码上修改如下代码即可:

fruit_item.xml



    

    

MainActivity.class
在LinearLayoutManager layoutManager = new LinearLayoutManager(this);下加下面代码即可
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);//设置横向滑动
这个要得益于LayoutManager布局系列接口,这就是为什么ListView不能实现横向滑动,
而RecyclerList能,因为前者的布局是自身管理,而后者是交给LayoutManager管理。

效果图:

                                                 


RecycleView的触发事件,相对ListView来说就复杂许多了,试问一下这有是为什么呢?
Recycle的优势那么明显干嘛不把监听也做得很简单呢?ListView是触发一个子项,但是当我们要触发子项当中的某一个按钮的时候咋办?
ListView也可以实现这样的监听,但是实现就会很复杂,所以RecycleView就直接摒弃了
触发子项,直接让触发事件又View来注册。

修改代码:FruitAdapter.class

package com.example.ldp.com.bean;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.ldp.com.recyclerview.R;

import java.util.List;

/**
 * Created by Administrator on 2017/3/6.
 */

public class FruitAdapter extends RecyclerView.Adapter {


    private List mfruitLists;
    static class ViewHolder extends RecyclerView.ViewHolder{
        View fruitView;
        ImageView fruitImage;
        TextView fruitName;
        public ViewHolder(View View){
            super(View);
            fruitView = View;
            fruitImage =(ImageView) View.findViewById(R.id.fruit_image);
            fruitName = (TextView) View.findViewById(R.id.fruit_name);
        }
    }

    public FruitAdapter(List fruitList){
        this.mfruitLists=fruitList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item,parent,false);
        final  ViewHolder holder = new ViewHolder(view);

        /*监听事件*/
        holder.fruitView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                Fruit fruit = mfruitLists.get(position);
                Toast.makeText(v.getContext(),"Click view  -"+fruit.getName(),Toast.LENGTH_SHORT).show();
            }
        });
        holder.fruitImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                Fruit fruit = mfruitLists.get(position);
                Toast.makeText(v.getContext(),"Click image  -",Toast.LENGTH_SHORT).show();
            }
        });
        holder.fruitName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                Fruit fruit = mfruitLists.get(position);
                Toast.makeText(v.getContext(),"Click name  -"+fruit.getName(),Toast.LENGTH_SHORT).show();
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Fruit fruit = mfruitLists.get(position);
        holder.fruitImage.setImageResource(fruit.getImageid());
        holder.fruitName.setText(fruit.getName());
    }

    @Override
    public int getItemCount() {

        return mfruitLists.size();
    }


}
对于数据的显示就学习到这里,ListView和RecycleView需要深入学习,这是一个复杂很难懂的知识点儿,说实话我也没学懂。

效果图:

                                                                         Androidc学习笔记二之四大布局及碎片理解用法_第2张图片


碎片
碎片是一种可以嵌入到活动当中的UI片段,它能让程序更加合理的去使用大屏幕空间
(这就有点儿类似web当中的响应式布局了)当我们在小屏幕上设计的美美的UI,到了大屏幕的
机器上就变得黑丑,全部被拉伸了。碎片的目的就是要充分合理的使用屏幕空间。
想象一下一个屏幕上左右两边各一个活动,点左边触动右边,是一件很爽的事儿哎。

概念图:

                                      Androidc学习笔记二之四大布局及碎片理解用法_第3张图片


在布局文件中我们使用来添加碎片,一个碎片就相当一有一个布局文件和一个继承了
Frament(重写onCreate方法),添加碎片到布局当中有两种方式,一种是静态添加(通过属性name="继承了Frame的类")
另外一种是通过活动Activity动态添加碎片。

例子:静态添加:

left_fragment.xml



    

LeftFragment.class
package com.example.ldp.com.fragmenttest;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2017/3/7.
 */

public class LeftFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.left_fragment,container,false);
        return view;
    }
}

right_fragment.xml



    


RightFragment.class
package com.example.ldp.com.fragmenttest;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * Created by Administrator on 2017/3/7.
 */

public class RightFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.right_fragment,container,false);
        return view;
    }
}

activity_main.xml



    
    
    

MainActivity这个活动不动

效果图:

                                 Androidc学习笔记二之四大布局及碎片理解用法_第4张图片
动态添加:
其他文件不动修改activity_main.xml




    
    
    

    
    




another_right_fragment.xml



    

AnotherRightFragment.class
package com.example.ldp.com.fragmenttest;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2017/3/7.
 */

public class AnotherRightFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.another_right_fragment,container,false);
        return view;
    }
}

修改MainActivity.class

package com.example.ldp.com.fragmenttest;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);//当前监听,已经通过实现onclickLister接口了
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                replaceFragment(new AnotherRightFragment());
                break;
            default:
                break;

        }
    }


    //最关键的方法就是使用replace()方法传入容器ID和碎片实例,提交事务
    private void replaceFragment(Fragment fragment){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.another_right_fragment,fragment);
        /**
         * 按下Back键返回到上一个碎片,如果没有这句代码,将直接退出程序
         * */
        fragmentTransaction.addToBackStack(null);
        /**
         * 在开发过程很重要
         * */
        fragmentTransaction.commit();
    }
}

效果图:

    Androidc学习笔记二之四大布局及碎片理解用法_第5张图片      Androidc学习笔记二之四大布局及碎片理解用法_第6张图片



你可能感兴趣的:(Androidc学习笔记二之四大布局及碎片理解用法)