PullToRefreshScrollView实现顶层搜索框 滑动可隐藏 下面刷自定义GridView

最近项目里有个需求,要求用GridView去显示商品,滑动到底部下拉能加载更多商品,向下滑动过程中需要隐藏掉自定义的Action(搜索框)布局,向上滑动能显示出来,整体效果类似淘宝APP搜索结果页那样。

起初觉得挺简单的,但是后来才发现还是得转一点脑子。最开始我想用PullToRefreshGridView,但是后来发现GridView没有添加headview的方法,只能采用PullToRefreshScrollView内嵌套GridView的方法,Scrollview里多放一个空白布局当GridView的headview,高度和Action布局一样就行。这时还有一个问题,ScrollView和GridView会产生滑动冲突,还好网上很容易找到解决办法,我这里采用自定义GridView,最后就是监听方法了,最为关键的点,还是直接上代码吧:

一、自定义GridView,解决ScrollView嵌套GridView布局加载数据不全问题

package com.hospital.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.GridView;

/**
* Created by Achen on 2016/7/23.
*/
public class MyGridView extends GridView{
public MyGridView(Context context) {
super(context);
  }

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

public MyGridView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}


二、布局
父布局

<?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:background="@color/white"
android:orientation="vertical"    <LinearLayout
           android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/dimens_7_160"
android:layout_marginRight="@dimen/dimens_7_160"
android:layout_marginTop="@dimen/dimens_3_160">

       <com.handmark.pulltorefresh.library.PullToRefreshScrollView
        xmlns:ptr="http://schemas.android.com/apk/res-auto"
        android:id="@+id/ptrf_sv_patient_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@color/gray"
        android:dividerHeight="2dp"
        android:fadingEdge="horizontal"
        android:fastScrollEnabled="false"
        android:footerDividersEnabled="true"
        android:headerDividersEnabled="true"
        android:smoothScrollbar="true"
        android:background="@color/background_activity_dialog"
        ptr:ptrMode="pullUpFromBottom" />

</LinearLayout>

</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" >

<LinearLayout
android:id="@+id/ll_ss_patient_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape"
android:gravity="center_horizontal"
android:focusable="true"
android:orientation="horizontal"
android:focusableInTouchMode="true"
android:layout_margin="@dimen/dimens_2_160">

<com.hospital.widget.EditTextSearch
android:id="@+id/et_ss_patient_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dimens_3_160"
android:layout_marginBottom="@dimen/dimens_3_160"
android:paddingLeft="@dimen/dimens_5_160"
android:paddingRight="@dimen/dimens_5_160"
android:drawableLeft="@drawable/ss"
android:drawablePadding="10dp"
android:hint="输入患者拼音首字母"
android:textColorHint="@color/color_tag_text_normal"
android:textColor="@color/color_tag_text_normal"
android:textSize="@dimen/dimens_8_160"
android:imeOptions="actionNext"
android:background="#00000000">

</com.hospital.widget.EditTextSearch>
</LinearLayout>

<com.hospital.widget.MyPullToRefreshGridView
android:id="@+id/gv_paitent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_activity_dialog"
android:horizontalSpacing="@dimen/dimens_4_160"
android:numColumns="1"
android:stretchMode="columnWidth"
android:verticalSpacing="@dimen/dimens_4_160" >
</com.hospital.widget.MyPullToRefreshGridView>

</LinearLayout>



三、代码
Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// 注册广播
registerBoradcastReceiver();

EventBus.getDefault().register(this);

View rootView = inflater.inflate(R.layout.patient_list, container, false);
View view = inflater.inflate(R.layout.patient_fragment_ptrfsv_item, null);

// 患者管理 搜索
ll_ss_patient_fragment = (LinearLayout) view.findViewById(R.id.ll_ss_patient_fragment);
et_ss_patient_fragment = (EditTextSearch) view.findViewById(R.id.et_ss_patient_fragment);

ll_ss_patient_fragment.setFocusable(true);
ll_ss_patient_fragment.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
et_ss_patient_fragment.setFocusable(true);
}
});

// 患者管理 条目
ptrf_sv_patient_fragment = (PullToRefreshScrollView) rootView.findViewById(R.id.ptrf_sv_patient_fragment);
ptrf_sv_patient_fragment.setMode(PullToRefreshBase.Mode.BOTH);

gv_paitent = (MyPullToRefreshGridView)view.findViewById(R.id.gv_paitent);
mPatientListAdapter = new PatientListAdapter(getActivity(), null);
gv_paitent.setAdapter(mPatientListAdapter);
ptrf_sv_patient_fragment.addView(view);

radioGroup = (RadioGroup) rootView.findViewById(R.id.rg_hzgl_patients);


// 关闭listitem_top_et软键盘
DesityUtil.closeKeyBoard(getActivity());

getPageNum();

// 获取患者分组标签名称
getPatientLable();

// 完整显示radioGroup
initRadioGroupAllView(sysLableList);

// 欠款患者分组统计
getPatientDetailsArrearsMoney();

// RadioGroup、EditText、ScrollView的监听事件
setListener();

// 根据CheckTag设置选中未选中状态、刷新gridView
refreshByCheckTag();

return rootView;
}

/**
* 监听
*/
private void setListener(){

// radioButton的点击监听
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
MonitorUserAction.addUserAction(MonitorUserAction.ACTIONPATIENTXXHZFZ);
// 分页恢复到默认
upOrDown = 1;
pageStart = 1;
// 搜索输入框失去焦点
et_ss_patient_fragment.clearFocus();
ll_ss_patient_fragment.requestFocus();
// Toast.makeText(getActivity(), "失去焦点", Toast.LENGTH_SHORT).show();
// 刷新患者列表
RadioButton tempButton2 = (RadioButton) getActivity().findViewById(checkedId);
if (tempButton2 != null) {
if (tempButton2.getTag().toString().equals("0")) { // 点击了全部患者选项
patientLabelId = "0";
getPatientDetails();
} else {
if (tempButton2.getTag().toString().equals("1")) { // 点击欠款患者选项
patientLabelId = "1";
getPatientDetailsArrears();
}else {
patientLabelId = tempButton2.getTag().toString(); // 点击标签分组患者
getPatientDetailsLable(patientLabelId);
}
}
}
// 遍历radiogroup里的button 设置字体颜色、背景
for (int i = 0; i < radioGroup.getChildCount(); i++) {
RadioButton tempButton1 = (RadioButton) group.getChildAt(i);
tempButton1.setTextColor(getResources().getColorStateList(R.color.prescription_gray_color));
tempButton1.setBackgroundResource(R.drawable.rb_bjys2);
if (radioGroup.getCheckedRadioButtonId() == tempButton1.getId()) {
rbCheckTag = tempButton1.getTag().toString();
tempButton1.setTextColor(getResources().getColor(R.color.white));
tempButton1.setBackgroundResource(R.drawable.rb_bjys);
}
}
// 关闭系统软键盘
DesityUtil.closeKeyBoard(getActivity());
}
});

// 搜索框的输入监听
et_ss_patient_fragment.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
// 分页恢复到默认
upOrDown = 1;
pageStart = 1;
String string = et_ss_patient_fragment.getText().toString();
if (string.length() != 0) {
getPatientListByShorthand(string);
}else {
getPatientDetails();
}
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub

}

@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub

}
});

// 搜索输入框焦点监听
et_ss_patient_fragment.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
if (b){
// 分页恢复到默认
upOrDown = 1;
pageStart = 1;
// 恢复全部患者
rb_hzgl_all.setChecked(true);
rbCheckTag = rb_hzgl_all.getTag().toString();
getPatientDetails();
// LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, listitem_top_et.getHeight());
// et_ss_patient_fragment.setLayoutParams(lp1);
// Toast.makeText(getActivity(), "获取焦点", Toast.LENGTH_SHORT).show();
}else {
// LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
// et_ss_patient_fragment.setLayoutParams(lp2);
// Toast.makeText(getActivity(), "失去焦点", Toast.LENGTH_SHORT).show();
}
}
});

// ScrollView的下拉刷新监听
ptrf_sv_patient_fragment.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
// 下拉刷新
upOrDown = 1;
pageStart = 1;
refreshByCheckTag();
}

@Override
public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
// 上拉加载
upOrDown = 0;
pageStart = pageStart + 1;
refreshByCheckTag();
}
});

}

具体可以再搭配查询本地数据库或者访问网络请求数据进行分页,
if (upOrDown == 1) {
mPatientListAdapter.updateDataClear(patientManageDTO);
}else {
mPatientListAdapter.updateDataNoClear(patientManageDTO);
}


adapter里关于数据源的设置updateDataClear与updateDataNoClear两个方法
package com.hospital.patient.adapter;

import java.text.ParseException;
import java.util.LinkedList;
import java.util.List;

import com.hospital.ConstantValue;
import com.hospital.R;
import com.hospital.log.error.MonitorUserAction;
import com.hospital.patient.PatientInfoEditActivity;
import com.hospital.patient.domain.PatientVO;
import com.hospital.patient.dto.PatientManageDTO;
import com.hospital.prescrible.PrescriptionActivity;
import com.hospital.utils.AgeUtils;
import com.hospital.utils.AnnotationClassUtil;
import com.hospital.utils.MoneyUtil;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
* 患者管理适配器
* @author wc
*/

public class PatientListAdapter extends BaseAdapter {

private Context context;
private List<PatientManageDTO> patients;
private LayoutInflater inflater;
ViewHolder viewHolder = null;

private int sex = 0;
private String sexLable = "";
public static String splitnei = "\\|\\|\\|";
public static String PATIENTLISTFRAGMENT = "patientListFragment";


public PatientListAdapter(Context context, List<PatientManageDTO> patients){
this.context = context;
if(patients == null){
this.patients = new LinkedList<>();
}else {
this.patients = patients;
}
inflater = LayoutInflater.from(context);
}

class ViewHolder {

}

@Override
public int getCount() {
// TODO Auto-generated method stub
return patients != null ? patients.size() : 0;
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return patients.get(position);
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub

return convertView;
}

/**
* 刷新数据
*/
public void updateDataClear(List<PatientManageDTO> patients){
if (patients != null){
this.patients.clear();
this.patients.addAll(patients);
// Log.i("tag", "==========updateDataClear" + this.patients.size());
notifyDataSetChanged();
}
}

/**
* 刷新数据,清空之前数据
*/
public void updateDataNoClear(List<PatientManageDTO> patients){
if (patients != null){
this.patients.addAll(patients);
// Log.i("tag", "==========updateDataNoClear" + this.patients.size());
notifyDataSetChanged();
}
}

}

 

至此,PullToRefreshScrollView可以实时上下拉刷新GridView,并可以实现搜索框上下滑动显示、隐藏的功能

 









转载于:https://www.cnblogs.com/achen0502/p/5852321.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ASP.NET GridView 控件可以通过绑定数据源来自动生成列,但如果需要自定义列的样式、行为或内容,可以使用自定义列的方式来实现。 以下是一些常见的自定义列方式: 1. TemplateField:通过在GridView 中添加 TemplateField 列,可以在列中添加自定义的 HTML 或控件。例如,可以在列中添加 LinkButton 或 Image 控件,以实现可点击的链接或图像。 2. BoundField:通过在 GridView 中添加 BoundField 列,可以设置列的数据绑定方式和格式化方式。例如,可以设置日期列的显示格式。 3. CommandField:通过在 GridView 中添加 CommandField 列,可以添加自定义的命令按钮,例如“编辑”、“删除”等。 4. HyperLinkField:通过在 GridView 中添加 HyperLinkField 列,可以实现类似于 TemplateField 的效果,但是只能添加超链接。 以下是一个示例,展示如何使用 TemplateField 和 BoundField 实现自定义列: ```asp <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"> <Columns> <asp:TemplateField HeaderText="Customer Name"> <ItemTemplate> <asp:Label ID="lblCustomerName" runat="server" Text='<%# Eval("CustomerName") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="OrderDate" HeaderText="Order Date" DataFormatString="{0:d}" /> <asp:TemplateField HeaderText="Order Total"> <ItemTemplate> <asp:Label ID="lblOrderTotal" runat="server" Text='<%# Eval("OrderTotal", "{0:c}") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> ``` 在上面的示例中,我们添加了三个列:一个使用 TemplateField 显示客户名称,一个使用 BoundField 显示订单日期,一个再次使用 TemplateField 显示订单总额。我们还使用 Eval 函数绑定了数据源中的字段。 注意,当使用自定义列时,需要设置 GridView 的 AutoGenerateColumns 属性为 False,否则 GridView 会自动创建列并覆盖我们添加的自定义列。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值