80,350
社区成员
发帖
与我相关
我的任务
分享
<?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;
}