碎片中手指向上滑动,加载更多数据问题

r00_a2lBUR 2018-10-17 03:56:05
我在做一个手指向上滑动,加载更多数据的Demo,数据不用翻页之前,是可以出来,但是数据需要翻页之后,就出不来了,只响应活动向上移动:

碎片的布局:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/ScrollView_All"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/LinearLayout_All"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center_vertical"
android:text="向上滑动获取更多"
/>
</LinearLayout>

</ScrollView>


碎片的代码:

import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class FragmentTest extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView =inflater.inflate(R.layout.fragment_test, container, false);

f_onCreate(rootView);

return rootView;
}

private void f_onCreate(View view){
mScrollViewAll=view.findViewById(R.id.ScrollView_All);
mLinearLayoutAll=view.findViewById(R.id.LinearLayout_All);

mOnTouchListener=new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int iActionID=event.getAction();
switch (iActionID){
case MotionEvent.ACTION_DOWN:
mfPosX=event.getX();
mfPosY=event.getY();
break;
case MotionEvent.ACTION_MOVE:
mfCurrentPosX=event.getX();
mfCurrentPosY=event.getY();
break;
case MotionEvent.ACTION_UP:
float fYMove=mfCurrentPosY-mfPosY;
float fAbsYMove=Math.abs(fYMove);
if(fAbsYMove<25)return true;
if(fYMove>0){
Toast.makeText(getActivity(),"向下滑动",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getActivity(),"向上滑动",Toast.LENGTH_SHORT).show();
f_move_up();
}
break;
}
return true;
}
};
view.setOnTouchListener(mOnTouchListener);
mLinearLayoutAll.setOnTouchListener(mOnTouchListener);

}

private void f_move_up(){
for(int i=0;i<10;i++){
if(miIndex>100)break;
TextView textView=new TextView(getActivity());
miIndex++;
String sText="项目:"+miIndex;
textView.setText(sText);
textView.setOnTouchListener(mOnTouchListener);
mLinearLayoutAll.addView(textView);
}
}

private View.OnTouchListener mOnTouchListener;
private ScrollView mScrollViewAll;
private LinearLayout mLinearLayoutAll;
//手势滑动位置
private float mfPosX=0.0f,mfPosY=0.0f,mfCurrentPosX=0.0f,mfCurrentPosY=0.0f;
private int miIndex=0;
}


主页面布局:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试一下碎片"
android:id="@+id/TextView_Title"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/TextView_Title"
android:text="打开碎片"
android:onClick="e_Button_Clicked"
android:id="@+id/Button_Open_Fragment"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/Button_Open_Fragment"
android:orientation="vertical"
android:id="@+id/LinearLayout_Fragment"
>

</LinearLayout>
</RelativeLayout>
</ScrollView>

主页代码:

public class MainActivity extends AppCompatActivity {

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

f_onCreate();
}

private void f_onCreate(){
mLinearLayoutFragment=findViewById(R.id.LinearLayout_Fragment);
}

private void f_open_fragment(){
FragmentTest fragmentTest=new FragmentTest();
getFragmentManager().beginTransaction()
.replace(R.id.LinearLayout_Fragment, fragmentTest).commit();
getFragmentManager().beginTransaction().show(fragmentTest);
}

public void e_Button_Clicked(View view){
int iViewID=view.getId();
switch (iViewID){
case R.id.Button_Open_Fragment:
f_open_fragment();
break;
}
}

private LinearLayout mLinearLayoutFragment;
}
...全文
519 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
SemiraChen 2018-10-19
  • 打赏
  • 举报
回复
上滑加载??为什么不用recyclerview万能适配器BaseRecyclerViewAdapterHelper https://github.com/CymChad/BaseRecyclerViewAdapterHelper 里面这个列表自带上滑加载的。
r00_a2lBUR 2018-10-18
  • 打赏
  • 举报
回复
引用 2 楼 YXTS122 的回复:
会不会是手机内 存暴涨,很卡,建议清理一下内存

不是内存的原因,是后面不响应onTouch了
YXTS122 2018-10-18
  • 打赏
  • 举报
回复
会不会是手机内 存暴涨,很卡,建议清理一下内存

80,350

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧